@Test public void shouldFailIfGivenNull() { validator.validate(null, errors); Assert.assertTrue(errors.hasErrors()); Assert.assertEquals("error.general", errors.getAllErrors().get(0).getCode()); }
@Test public void validate_shouldPassValidationIfAllRequiredValuesAreSet() { attributeType.setName("name"); attributeType.setMinOccurs(1); attributeType.setDatatypeClassname(RegexValidatedTextDatatype.class.getName()); attributeType.setDatatypeConfig("[a-z]+"); validator.validate(attributeType, errors); Assert.assertFalse(errors.hasErrors()); } }
@Test public void validate_shouldPassValidationIfFieldLengthsAreCorrect() { attributeType.setName("name"); attributeType.setMinOccurs(1); attributeType.setDatatypeClassname(RegexValidatedTextDatatype.class.getName()); attributeType.setDatatypeConfig("[a-z]+"); attributeType.setHandlerConfig("HandlerConfig"); validator.validate(attributeType, errors); Assert.assertFalse(errors.hasErrors()); }
/** * @see org.openmrs.api.FormService#saveForm(org.openmrs.Form) */ @Override public Form saveForm(Form form) throws APIException { checkIfFormsAreLocked(); BindException errors = new BindException(form, "form"); formValidator.validate(form, errors); if (errors.hasErrors()) { throw new APIException(errors); } if (form.getFormFields() != null) { for (FormField ff : form.getFormFields()) { if (ff.getForm() == null) { ff.setForm(form); } else if (!ff.getForm().equals(form)) { throw new APIException("Form.contains.FormField.error", new Object[] { ff }); } } } return dao.saveForm(form); }
/** * @see PatientProgramValidator#validate(Object,Errors) */ @Test public void validate_shouldPassValidationIfFieldLengthsAreCorrect() { ProgramWorkflowService pws = Context.getProgramWorkflowService(); Patient patient = Context.getPatientService().getPatient(6); PatientProgram pp = new PatientProgram(); pp.setPatient(patient); pp.setProgram(pws.getProgram(1)); pp.setDateEnrolled(new Date()); pp.setVoidReason("voidReason"); BindException errors = new BindException(pp, "program"); new PatientProgramValidator().validate(pp, errors); Assert.assertEquals(false, errors.hasErrors()); }
/** * Validate all pages and process finish. * If there are page validation errors, show the corresponding view page. * @see #validatePagesAndFinish */ private ModelAndView renderValidatePagesAndFinish( RenderRequest request, RenderResponse response, Object command, BindException errors, int currentPage) throws Exception { // In case of any errors -> show current page. if (errors.hasErrors()) return showPage(request, errors, currentPage); // No remaining errors -> proceed with finish. return renderFinish(request, response, command, errors); }
@RequestMapping(value="/containers/{containerName:.+}/definitions/{definitionName:.+}/new", method=RequestMethod.POST) public String saveInstanceDefinition(Model model, HttpServletRequest request, @PathVariable String containerName, @PathVariable String definitionName) { ContainerManager cm = serviceManagerHelper.findContainerManagerByContainerName(containerName); ServiceDeployment sd = retrieveServiceDeployment(request); ServiceDefinition serviceDefinition = getServiceDefinition(definitionName, cm); BindException errors = new BindException(sd, MODEL_SERVICE_DEPLOYMENT); new ServiceDeploymentValidator(serviceDefinition, !StringUtils.equals(request.getParameter("type"), SERVICE_DEPLOYMENT_TYPE_FORM)).validate(sd, errors); if (errors.hasErrors()) { Object targetServiceDeployment = transformServiceDeployment(sd, request.getParameter("type")); fillModel(model, targetServiceDeployment, sd.getServiceId(), serviceDefinition, containerName, MODEL_OPERATION_TYPE_NEW); model.addAttribute(BindingResult.MODEL_KEY_PREFIX + MODEL_SERVICE_DEPLOYMENT, errors); return "console.platform.container.definition.new." + request.getParameter("type"); } DeploymentResult deployResult = cm.deployNewService(sd); model.addAttribute("deployResults", Collections.singletonMap(sd.getServiceId(), deployResult)); model.addAttribute("backUrl", "/containers/" + containerName + "/services"); return "console.platform.services.redeploy.results"; }
/** * This implementation calls <code>showForm</code> in case of errors, * and delegates to <code>onSubmitRender<code>'s full version else. * <p>This can only be overridden to check for an action that should be executed * without respect to binding errors, like a cancel action. To just handle successful * submissions without binding errors, override one of the <code>onSubmitRender</code> * methods. * @see #showForm(RenderRequest, RenderResponse, BindException) * @see #onSubmitRender(RenderRequest, RenderResponse, Object, BindException) * @see #onSubmitRender(Object, BindException) * @see #onSubmitRender(Object) * @see #processFormSubmission(ActionRequest, ActionResponse, Object, BindException) */ @Override protected ModelAndView renderFormSubmission(RenderRequest request, RenderResponse response, Object command, BindException errors) throws Exception { if (errors.hasErrors() || isFormChangeRequest(request)) { return showForm(request, response, errors); } else { return onSubmitRender(request, response, command, errors); } }
@RequestMapping(value="/containers/{containerName:.+}/services/{serviceId:.+}/edit", method=RequestMethod.POST) public String editServicePropertiesComplete(Model model, HttpServletRequest request, @PathVariable String containerName, @PathVariable String serviceId) { ContainerManager cm = serviceManagerHelper.findContainerManagerByContainerName(containerName); ServiceDeployment sd = retrieveServiceDeployment(request); ServiceDefinition serviceDefinition = getServiceDefinition(sd.getServiceDefinitionId(), cm); BindException errors = new BindException(sd, MODEL_SERVICE_DEPLOYMENT); new ServiceDeploymentValidator(serviceDefinition, !StringUtils.equals(request.getParameter("type"), SERVICE_DEPLOYMENT_TYPE_FORM)).validate(sd, errors); if (errors.hasErrors()) { Object targetServiceDeployment = transformServiceDeployment(sd, request.getParameter("type")); fillModel(model, targetServiceDeployment, sd.getServiceId(), serviceDefinition, containerName, MODEL_OPERATION_TYPE_NEW); model.addAttribute(BindingResult.MODEL_KEY_PREFIX + MODEL_SERVICE_DEPLOYMENT, errors); return "console.platform.container.definition.new." + request.getParameter("type"); } DeploymentResult deployResult; if (cm.undeployService(sd.getServiceId())) { deployResult = cm.deployNewService(sd); } else { deployResult = new DeploymentResult("Error while undeploying service"); } model.addAttribute("deployResults", Collections.singletonMap(sd.getServiceId(), deployResult)); model.addAttribute("backUrl", "/containers/" + containerName + "/services"); return "console.platform.services.redeploy.results"; }
/** * Treats errors as fatal. Use this method only if * it's an error if the input isn't valid. * This might be appropriate if all input is from dropdowns, for example. * @throws ServletRequestBindingException subclass of ServletException on any binding problem */ public void closeNoCatch() throws ServletRequestBindingException { if (getErrors().hasErrors()) { throw new ServletRequestBindingException( "Errors binding onto object '" + getErrors().getObjectName() + "'", getErrors()); } }
/** * Bind incoming request parameters to allowed fields of the form object. * <p> * NOTE: This action method is not designed to be overidden and might become <code>final</code> in a future * version of Spring Web Flow. If you need to execute custom data binding logic have your flow call this method * along with your own custom methods as part of a single action chain. Alternatively, override the * {@link #doBind(RequestContext, DataBinder)} hook. * @param context the action execution context, for accessing and setting data in "flow scope" or "request scope" * @return "success" if there are no binding errors, "error" otherwise * @throws Exception an <b>unrecoverable</b> exception occured, either checked or unchecked */ public Event bind(RequestContext context) throws Exception { if (logger.isDebugEnabled()) { logger.debug("Executing bind"); } Object formObject = getFormObject(context); DataBinder binder = createBinder(context, formObject); doBind(context, binder); putFormErrors(context, binder.getErrors()); return binder.getErrors().hasErrors() ? error() : success(); }
return binder.getErrors().hasErrors() ? error() : success();
if (!errors.hasErrors() || (this.allowDirtyBack && targetPage < currentPage) || (this.allowDirtyForward && targetPage > currentPage)) {
if (errors.hasErrors()) { logger.debug("Form submission errors: " + errors.getErrorCount()); HttpServletHelper.getInstance().reloadApplicationMap(request, application);
throws Exception { if (errors.hasErrors()) { if (logger.isDebugEnabled()) { logger.debug("Data binding errors: " + errors.getErrorCount());
if (errors.hasErrors()) { result = showForm(request, response, errors, errors.getModel());
/** * Validate all pages and process finish. * If there are page validation errors, show the corresponding view page. * @see #renderValidatePagesAndFinish */ private void validatePagesAndFinish( ActionRequest request, ActionResponse response, Object command, BindException errors, int currentPage) throws Exception { // In case of binding errors -> show current page. if (errors.hasErrors()) { setPageRenderParameter(response, currentPage); passRenderParameters(request, response); return; } if (!suppressValidation(request)) { // In case of remaining errors on a page -> show the page. for (int page = 0; page < getPageCount(request, command); page++) { validatePage(command, errors, page, true); if (errors.hasErrors()) { setPageRenderParameter(response, currentPage); passRenderParameters(request, response); return; } } } // No remaining errors -> proceed with finish. if (!isRedirectAction()) setPageRenderParameter(response, currentPage); processFinish(request, response, command, errors); }
if (errors.hasErrors()) { result = showForm(request, response, errors, errors.getModel());
if (errors.hasErrors()) { result = showForm(request, response, errors, errors.getModel());
if (errors.hasErrors()) { result = showForm(request, response, errors, errors.getModel());