List of usage examples for org.springframework.validation FieldError FieldError
public FieldError(String objectName, String field, @Nullable Object rejectedValue, boolean bindingFailure, @Nullable String[] codes, @Nullable Object[] arguments, @Nullable String defaultMessage)
From source file:org.pdfgal.pdfgalweb.utils.impl.PDFGalWebUtilsImpl.java
@Override public FieldError createDefaultFieldError(final String objectName, final String field, final Object rejectedValue, final String code) { final String[] codes = new String[1]; codes[0] = code;//from w w w. j av a 2 s . c o m return new FieldError(objectName, field, rejectedValue, false, codes, null, null); }
From source file:org.easyj.rest.controller.GenericEntityController.java
public <T> void bindValidatorErrors(Set<ConstraintViolation<T>> violations, BindingResult result) { if (violations != null) { for (ConstraintViolation<T> violation : violations) { result.addError(new FieldError(result.getObjectName(), violation.getPropertyPath().toString(), violation.getInvalidValue(), false, null, null, violation.getMessageTemplate())); }/*from w w w.ja v a 2s . c om*/ } }
From source file:info.gehrels.voting.web.VoteBuilder.java
public void validate(String objectName, String fieldPrefix, BindingResult bindingResult) { if (type == VoteType.PREFERENCE) { if (allNullOrEmpty(preferencesByCandidateIdx)) { bindingResult.addError(new FieldError(objectName, fieldPrefix + ".preferencesByCandidateIdx", "", true, new String[] { "emptyPreference" }, new Object[] {}, "You must enter a preference if you have selected it.")); } else if (containsDuplicates(preferencesByCandidateIdx)) { bindingResult.addError(new FieldError(objectName, fieldPrefix + ".preferencesByCandidateIdx", "", true, new String[] { "duplicatesInPreference" }, new Object[] {}, "You must enter a duplicate free preference.")); }// w w w . j a va 2s. com } else { if (!allNullOrEmpty(preferencesByCandidateIdx)) { bindingResult.addError(new FieldError(objectName, fieldPrefix + ".preferencesByCandidateIdx", "", true, new String[] { "noPreferenceAllowed" }, new Object[] {}, "You must not enter a preference for no, invalid or non casted votes.")); } } }
From source file:com.mitchellbosecke.pebble.spring.PebbleViewResolverTest.java
private void initBindingResultFieldErrors() { when(this.bindingResult.hasFieldErrors("testField")).thenReturn(true); List<FieldError> fieldErrors = new ArrayList<>(); fieldErrors/*from w ww . ja va 2s . com*/ .add(new FieldError(FORM_NAME, "testField", null, false, new String[] { "error.field.test.params" }, new String[] { "param1", "param2" }, "???error.field.test.params???")); when(this.bindingResult.getFieldErrors("testField")).thenReturn(fieldErrors); }
From source file:org.jblogcms.core.blog.controller.EditBlogController.java
/** * Returns saved blog, if exceptions occurs translates them into {@link org.springframework.validation.FieldError}s * and adds those errors to the {@code BindingResult} object * * @param blogForm the new blog//from www.j a va 2 s .co m * @param result the {@code BindingResult} object * @return the saved blog */ private Blog editBlog(Blog blogForm, BindingResult result) { Blog blog = null; try { blog = blogService.editBlog(blogForm); } catch (DuplicateBlogNameException e) { String errorCode = messageSource.getMessage(e.getLocalMessage(), null, null); FieldError error = new FieldError("blog", "name", blogForm.getName(), false, new String[] { errorCode }, new Object[] {}, errorCode); result.addError(error); } catch (DuplicateBlogUrlNameException e) { String errorCode = messageSource.getMessage(e.getLocalMessage(), null, null); FieldError error = new FieldError("blog", "urlName", blogForm.getUrlName(), false, new String[] { errorCode }, new Object[] {}, errorCode); result.addError(error); } return blog; }
From source file:org.easyj.rest.controller.AbstractController.java
@ResponseStatus(value = HttpStatus.BAD_REQUEST) @ExceptionHandler(TypeMismatchException.class) public ModelAndView handleTypeMismatch(TypeMismatchException e) { BindingResult result = createBindingResult(getClass()); result.addError(new FieldError("param.bind", e.getRequiredType().getSimpleName(), e.getValue(), true, null, null, "error.param.bind.type")); return configMAV(null, result, "errors/badrequest"); }
From source file:com.br.uepb.validator.adapter.CustomConstraintSpringValidatorAdapter.java
@Override protected void processConstraintViolations(Set<ConstraintViolation<Object>> violations, Errors errors) { for (ConstraintViolation<Object> violation : violations) { String field = violation.getPropertyPath().toString(); FieldError fieldError = errors.getFieldError(field); if (fieldError == null || !fieldError.isBindingFailure()) { try { String errorCode = violation.getConstraintDescriptor().getAnnotation().annotationType() .getSimpleName(); Object[] errorArgs = getArgumentsForConstraint(errors.getObjectName(), field, violation.getConstraintDescriptor()); 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[] errorCodes = bindingResult.resolveMessageCodes(errorCode, field); String nestedField = bindingResult.getNestedPath() + field; ObjectError error;/*from ww w .j a v a2 s . c o m*/ if ("".equals(nestedField)) { error = new ObjectError(errors.getObjectName(), errorCodes, errorArgs, violation.getMessage()); } else { Object invalidValue = violation.getInvalidValue(); if (!"".equals(field) && invalidValue == violation.getLeafBean()) { // bean constraint with property path: retrieve the actual property value invalidValue = bindingResult.getRawFieldValue(field); } if (violation.getMessage() != null && violation.getMessage().startsWith("{") && violation.getMessage().endsWith("}")) { String keyMessage = violation.getMessage(); keyMessage = keyMessage.replace("{", ""); keyMessage = keyMessage.replace("}", ""); List<String> temp = new ArrayList<String>(Arrays.asList(errorCodes)); temp.add(keyMessage); errorCodes = temp.toArray(new String[temp.size()]); error = new FieldError(errors.getObjectName(), nestedField, invalidValue, false, errorCodes, errorArgs, violation.getMessage()); } else { error = new FieldError(errors.getObjectName(), nestedField, invalidValue, false, errorCodes, errorArgs, violation.getMessage()); } } bindingResult.addError(error); } else { // got no BindingResult - can only do standard rejectValue call // with automatic extraction of the current field value if (violation.getMessage() != null && violation.getMessage().startsWith("{") && violation.getMessage().endsWith("}")) { String keyMessage = violation.getMessage(); keyMessage = keyMessage.replace("{", ""); keyMessage = keyMessage.replace("}", ""); errors.rejectValue(field, keyMessage); } else { 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); } } } }
From source file:com.sopovs.moradanen.fan.controller.SignupController.java
@RequestMapping(value = "/signup", method = RequestMethod.POST) public String signup(@Valid SignupData signupData, BindingResult bindingResult, ModelMap modelMap, WebRequest request) {/*w w w.j ava 2 s . c o m*/ if (!userDetailService.checkUsernameNotUsed(signupData.getUserName())) { bindingResult.addError(new FieldError("registrationData", "userName", signupData.getUserName(), false, null, null, "username already in use")); } if (!userDetailService.checkVisibleNameNotUsed(signupData.getVisibleName())) { bindingResult.addError(new FieldError("registrationData", "visibleName", signupData.getVisibleName(), false, null, null, "visible name already in use")); } if (bindingResult.hasErrors()) { modelMap.put("signupData", signupData); return "chooseUserName"; } User newUser = signupData.createUser(); userDetailService.saveUser(newUser); SignInUtils.signin(newUser.getId().toString()); providerSignInUtils.doPostSignUp(newUser.getId().toString(), request); return "redirect:/"; }
From source file:org.jblogcms.core.account.controller.EditAccountController.java
/** * Returns saved account, if exceptions occurs translates them into {@link org.springframework.validation.FieldError}s * and adds those errors to the {@code BindingResult} object * * @param accountForm the new account/*from ww w . jav a2 s. c o m*/ * @param result the {@code BindingResult} object * @return the saved account */ private Account editAccount(Account accountForm, BindingResult result) { Account account = null; try { account = accountService.editAccount(accountForm); } catch (DuplicateEmailException e) { String errorCode = messageSource.getMessage(e.getLocalMessage(), null, null); FieldError error = new FieldError("user", "email", accountForm.getEmail(), false, new String[] { errorCode }, new Object[] {}, errorCode); result.addError(error); } return account; }
From source file:org.jblogcms.core.blog.controller.AddBlogController.java
/** * Returns saved blog, if exceptions occurs translates them into {@link org.springframework.validation.FieldError}s * and adds those errors to the {@code BindingResult} * * @param blogForm the new blog//w ww.j a v a 2s . c o m * @param currentAccountId the primary key of the account, blog creator * @param result the {@code BindingResult} object * @return the saved blog */ private Blog addBlog(Blog blogForm, Long currentAccountId, BindingResult result) { Blog blog = null; try { blog = blogService.addBlog(blogForm, currentAccountId); } catch (DuplicateBlogNameException e) { String errorCode = messageSource.getMessage(e.getLocalMessage(), null, null); FieldError error = new FieldError("blog", "name", blogForm.getName(), false, new String[] { errorCode }, new Object[] {}, errorCode); result.addError(error); } catch (DuplicateBlogUrlNameException e) { String errorCode = messageSource.getMessage(e.getLocalMessage(), null, null); FieldError error = new FieldError("blog", "urlName", blogForm.getUrlName(), false, new String[] { errorCode }, new Object[] {}, errorCode); result.addError(error); } return blog; }