Example usage for org.springframework.validation FieldError getField

List of usage examples for org.springframework.validation FieldError getField

Introduction

In this page you can find the example usage for org.springframework.validation FieldError getField.

Prototype

public String getField() 

Source Link

Document

Return the affected field of the object.

Usage

From source file:com.wisemapping.rest.model.RestErrors.java

public Map<String, String> getFieldErrors() {
    Locale locale = LocaleContextHolder.getLocale();
    final Map<String, String> result = new HashMap<String, String>();
    if (errors != null) {
        final List<FieldError> fieldErrors = errors.getFieldErrors();
        for (FieldError fieldError : fieldErrors) {
            result.put(fieldError.getField(), messageSource.getMessage(fieldError, locale));
        }//from   w w w  .j a  va 2s.  c  om
    }
    return result;
}

From source file:cz.swi2.mendeluis.app.web.controllers.RegistrationController.java

/**
 * Handles registration form submission. 
 *//*from   w  w  w.j  a v  a2s  . c o m*/
@RequestMapping(method = RequestMethod.POST)
public String create(@ModelAttribute("user") @Validated UserRegistrationDTO user, BindingResult bindingResult,
        Model model, RedirectAttributes redirectAttributes, UriComponentsBuilder uriBuilder) {

    log.info("submitted registration: {}", user);
    if (bindingResult.hasErrors()) {
        for (ObjectError ge : bindingResult.getGlobalErrors()) {
            log.info("ObjectError: {}", ge);
        }
        for (FieldError fe : bindingResult.getFieldErrors()) {
            model.addAttribute(fe.getField() + "_error", true);
            log.info("FieldError: {}", fe);
        }
        model.addAttribute("alert_failure", "There are some errors with submitted data.");
        return "user/registration";
    } else {
        try {
            userFacade.createNewUser(user.getName(), user.getUsername(), user.getPassword());
            redirectAttributes.addFlashAttribute("alert_success", "You sign up. You can log in now!");
            return "redirect:/login";
        } catch (UniqueViolationException e) {
            model.addAttribute("alert_failure", "The username already exists!");
            return "user/registration";
        }
    }
}

From source file:com.hadoopvietnam.controller.member.ChangePasswordController.java

@PreAuthorize("isAuthenticated()")
@RequestMapping(value = { "/json" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
@ResponseBody/*w w w  .  java 2s.c  om*/
public ValidationResponse changePasswordAjaxJson(Model model,
        @ModelAttribute("changePasswordForm") ChangePasswordForm form, HttpServletRequest request,
        BindingResult bindingResult) {
    ValidationResponse res = new ValidationResponse();
    this.validator.validate(form, bindingResult);
    if (!bindingResult.hasErrors()) {
        res.setStatus("SUCCESS");
    } else {
        res.setStatus("FAIL");
        List<FieldError> allErrors = bindingResult.getFieldErrors();
        List<ErrorMessage> errorMesages = new ArrayList<ErrorMessage>();
        for (FieldError objectError : allErrors) {
            errorMesages.add(new ErrorMessage(objectError.getField(),
                    this.resourceMessage.getMessage(objectError.getCode(), request)));
        }
        res.setErrorMessageList(errorMesages);
    }
    return res;
}

From source file:org.exoplatform.acceptance.rest.JsonErrorHandler.java

/**
 * Catch MethodArgumentNotValidException when a Bean Validation error occurs.
 *
 * @param ex The exception trapped//from   w ww. j a  v  a2 s  . c  om
 * @return A standardized {@link org.exoplatform.acceptance.rest.JsonErrorResponse}
 * @throws java.io.IOException if any.
 */
@ExceptionHandler(MethodArgumentNotValidException.class)
@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public JsonErrorResponse processValidationError(MethodArgumentNotValidException ex) throws IOException {
    LOGGER.warn("Validation error : {}", ex.getMessage());
    List<FieldError> fieldErrors = ex.getBindingResult().getFieldErrors();
    List<ObjectError> globalErrors = ex.getBindingResult().getGlobalErrors();
    JsonErrorResponse errors = new JsonErrorResponse();
    for (FieldError fieldError : fieldErrors) {
        errors.addFieldError(fieldError.getField(), fieldError.getDefaultMessage());
    }
    for (ObjectError globalError : globalErrors) {
        errors.addGlobalError(globalError.getObjectName(), globalError.getDefaultMessage());
    }
    return errors;
}

From source file:com.fengduo.bee.web.controller.BaseController.java

/**
 * ??// w  w  w  .  j  a v  a  2 s.co  m
 * 
 * @param result
 * @return
 */
public Result showErrors(BindingResult result) {
    StringBuffer errorsb = new StringBuffer();
    if (result.hasErrors()) {
        for (FieldError error : result.getFieldErrors()) {
            errorsb.append(error.getField());
            errorsb.append(error.getDefaultMessage());
            errorsb.append("|");
        }
        String errorsr = errorsb.toString().substring(0, errorsb.toString().length() - 1);
        return Result.failed(errorsr.replaceAll("null", StringUtils.EMPTY));
    }
    return Result.success();
}

From source file:com.rockagen.gnext.controller.ApplicationController.java

/**
 * Handle errors/*from  w  w w . j a  va  2 s  .co m*/
 *
 * @param errors
 * @return
 */
public ErrorMsg error(Errors errors) {
    FieldError e = errors.getFieldError();
    ErrorMsg em = new ErrorMsg();
    em.field = e.getField();
    em.message = e.getDefaultMessage();
    em.value = e.getRejectedValue();
    return em;

}

From source file:cz.fi.muni.pa036.airticketbooking.rest.handler.RestErrorHandler.java

private ValidationErrorDTO processFieldErrors(List<FieldError> fieldErrors) {
    ValidationErrorDTO dto = new ValidationErrorDTO();

    for (FieldError fieldError : fieldErrors) {
        String localizedErrorMessage = resolveLocalizedErrorMessage(fieldError);
        dto.addFieldError(fieldError.getField(), localizedErrorMessage);
    }/*from ww  w . j a v  a  2s  .c  o m*/

    return dto;
}

From source file:com.google.ie.common.validation.CommentValidator.java

public void validateIdeaComments(Object object, Errors errors) {
    IdeaComment ideaComment = (IdeaComment) object;

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, IDEA_KEY, IdeaExchangeConstants.Messages.REQUIRED_FIELD);
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, TEXT, IdeaExchangeConstants.Messages.REQUIRED_FIELD);
    if (StringUtils.length(ideaComment.getText()) > COMMENT_LENGTH) {
        errors.rejectValue(TEXT, IdeaExchangeErrorCodes.COMMENT_LENGTH_EXCEEDS,
                IdeaExchangeConstants.Messages.COMMENT_LENGTH_EXCEEDS);
    }/* w w w.  ja v a 2s.  com*/

    if (log.isDebugEnabled()) {
        if (errors.hasErrors()) {
            log.debug("Validator found " + errors.getErrorCount() + " errors");
            for (Iterator<FieldError> iterator = errors.getFieldErrors().iterator(); iterator.hasNext();) {
                FieldError fieldError = iterator.next();
                log.debug("Error found in field: " + fieldError.getField() + " Message :"
                        + fieldError.getDefaultMessage());
            }
        } else {
            log.debug("Validator found no errors");
        }

    }
}

From source file:com.google.ie.common.validation.CommentValidator.java

public void validateProjectComments(Object object, Errors errors) {

    ProjectComment projectComment = (ProjectComment) object;

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, PROJECT_KEY,
            IdeaExchangeConstants.Messages.REQUIRED_FIELD);
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, TEXT, IdeaExchangeConstants.Messages.REQUIRED_FIELD);
    if (StringUtils.length(projectComment.getText()) > COMMENT_LENGTH) {
        errors.rejectValue(TEXT, IdeaExchangeErrorCodes.COMMENT_LENGTH_EXCEEDS,
                IdeaExchangeConstants.Messages.COMMENT_LENGTH_EXCEEDS);
    }/*from   w ww  .  j a va  2  s  .  c o  m*/

    if (log.isDebugEnabled()) {
        if (errors.hasErrors()) {
            log.debug("Validator found " + errors.getErrorCount() + " errors");
            for (Iterator<FieldError> iterator = errors.getFieldErrors().iterator(); iterator.hasNext();) {
                FieldError fieldError = iterator.next();
                log.debug("Error found in field: " + fieldError.getField() + " Message :"
                        + fieldError.getDefaultMessage());
            }
        } else {
            log.debug("Validator found no errors");
        }

    }
}

From source file:de.extra.client.core.util.impl.ExtraValidator.java

private String convertToString(final Errors errors) {
    final List<FieldError> fieldErrors = errors.getFieldErrors();
    final StringBuilder stringBuilder = new StringBuilder(fieldErrors.size() + " Konfigurationsfehler: ");
    final String sep = ";";
    for (final FieldError fieldError : fieldErrors) {
        stringBuilder.append(fieldError.getObjectName());
        stringBuilder.append(".").append(fieldError.getField());
        stringBuilder.append(" ").append(fieldError.getDefaultMessage());
        stringBuilder.append(sep);//from www .  ja v  a 2  s .  c o m
    }
    return stringBuilder.toString();
}