use of org.springframework.validation.ObjectError in project head by mifos.
the class DefineProductCategoryPreviewController method processFormSubmit.
@RequestMapping(method = RequestMethod.POST)
public ModelAndView processFormSubmit(@RequestParam(value = CANCEL_PARAM, required = false) String cancel, @RequestParam(value = EDIT_PARAM, required = false) String edit, @ModelAttribute("formBean") ProductCategoryFormBean bean, BindingResult result) {
ModelAndView modelAndView = new ModelAndView("defineNewCategory");
if (StringUtils.isNotBlank(cancel)) {
modelAndView.setViewName(REDIRECT_TO_ADMIN);
} else if (StringUtils.isNotBlank(edit)) {
modelAndView.setViewName("defineNewCategory");
modelAndView.addObject("formBean", bean);
modelAndView.addObject("typeList", this.getProductCategoryTypes());
} else if (result.hasErrors()) {
modelAndView.setViewName("newProductCategoryPreview");
modelAndView.addObject("formBean", bean);
} else {
CreateOrUpdateProductCategory productCategory = new CreateOrUpdateProductCategory(Short.parseShort(bean.getProductTypeId()), bean.getProductCategoryName(), bean.getProductCategoryDesc(), Short.parseShort(bean.getProductCategoryStatusId()), bean.getGlobalPrdCategoryNum());
try {
this.adminServiceFacade.createProductCategory(productCategory);
modelAndView.setViewName(REDIRECT_TO_ADMIN);
} catch (BusinessRuleException e) {
ObjectError error = new ObjectError("formBean", new String[] { e.getMessageKey() }, new Object[] {}, "default: ");
result.addError(error);
modelAndView.setViewName("newProductCategoryPreview");
modelAndView.addObject("formBean", bean);
}
}
return modelAndView;
}
use of org.springframework.validation.ObjectError in project head by mifos.
the class DeleteCoaController method processFormSubmit.
@RequestMapping(method = RequestMethod.POST)
public ModelAndView processFormSubmit(@RequestParam(value = CANCEL_PARAM, required = false) String cancel, @Valid @ModelAttribute("formBean") CoaFormBean formBean, BindingResult result, SessionStatus status) {
ModelAndView mav = new ModelAndView(REDIRECT_TO_COA_ADMIN_SCREEN);
if (StringUtils.isNotBlank(cancel)) {
status.setComplete();
} else {
try {
coaServiceFacade.delete(formBean.getAccountId());
status.setComplete();
} catch (BusinessRuleException ex) {
ObjectError error = new ObjectError("formBean", new String[] { ex.getMessageKey() }, new Object[] {}, "default: ");
result.addError(error);
mav.setViewName(DELETE_COA);
mav.addObject("formBean", formBean);
}
}
return mav;
}
use of org.springframework.validation.ObjectError in project spring-boot by spring-projects.
the class YamlConfigurationFactory method validate.
private void validate() throws BindException {
BindingResult errors = new BeanPropertyBindingResult(this.configuration, "configuration");
this.validator.validate(this.configuration, errors);
if (errors.hasErrors()) {
logger.error("YAML configuration failed validation");
for (ObjectError error : errors.getAllErrors()) {
logger.error(getErrorMessage(error));
}
throw new BindException(errors);
}
}
use of org.springframework.validation.ObjectError in project spring-framework by spring-projects.
the class SpringValidatorAdapter method processConstraintViolations.
/**
* Process the given JSR-303 ConstraintViolations, adding corresponding errors to
* the provided Spring {@link Errors} object.
* @param violations the JSR-303 ConstraintViolation results
* @param errors the Spring errors object to register to
*/
protected void processConstraintViolations(Set<ConstraintViolation<Object>> violations, Errors errors) {
for (ConstraintViolation<Object> violation : violations) {
String field = determineField(violation);
FieldError fieldError = errors.getFieldError(field);
if (fieldError == null || !fieldError.isBindingFailure()) {
try {
ConstraintDescriptor<?> cd = violation.getConstraintDescriptor();
String errorCode = determineErrorCode(cd);
Object[] errorArgs = getArgumentsForConstraint(errors.getObjectName(), field, cd);
if (errors instanceof BindingResult) {
// Can do custom FieldError registration with invalid value from ConstraintViolation,
// as necessary for Hibernate Validator compatibility (non-indexed set path in field)
BindingResult bindingResult = (BindingResult) errors;
String nestedField = bindingResult.getNestedPath() + field;
if ("".equals(nestedField)) {
String[] errorCodes = bindingResult.resolveMessageCodes(errorCode);
bindingResult.addError(new ObjectError(errors.getObjectName(), errorCodes, errorArgs, violation.getMessage()));
} else {
Object rejectedValue = getRejectedValue(field, violation, bindingResult);
String[] errorCodes = bindingResult.resolveMessageCodes(errorCode, field);
bindingResult.addError(new FieldError(errors.getObjectName(), nestedField, rejectedValue, false, errorCodes, errorArgs, violation.getMessage()));
}
} else {
// got no BindingResult - can only do standard rejectValue call
// with automatic extraction of the current field value
errors.rejectValue(field, errorCode, errorArgs, violation.getMessage());
}
} catch (NotReadablePropertyException ex) {
throw new IllegalStateException("JSR-303 validated property '" + field + "' does not have a corresponding accessor for Spring data binding - " + "check your DataBinder's configuration (bean property versus direct field access)", ex);
}
}
}
}
use of org.springframework.validation.ObjectError in project spring-framework by spring-projects.
the class ValidatorFactoryTests method testSpringValidationWithClassLevel.
@Test
public void testSpringValidationWithClassLevel() throws Exception {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.afterPropertiesSet();
ValidPerson person = new ValidPerson();
person.setName("Juergen");
person.getAddress().setStreet("Juergen's Street");
BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person");
validator.validate(person, result);
assertEquals(1, result.getErrorCount());
ObjectError globalError = result.getGlobalError();
List<String> errorCodes = Arrays.asList(globalError.getCodes());
assertEquals(2, errorCodes.size());
assertTrue(errorCodes.contains("NameAddressValid.person"));
assertTrue(errorCodes.contains("NameAddressValid"));
}
Aggregations