Example usage for org.springframework.validation BindingResult getFieldErrors

List of usage examples for org.springframework.validation BindingResult getFieldErrors

Introduction

In this page you can find the example usage for org.springframework.validation BindingResult getFieldErrors.

Prototype

List<FieldError> getFieldErrors();

Source Link

Document

Get all errors associated with a field.

Usage

From source file:org.zht.framework.validate.ValidateHandler.java

public static String getDefaultError(BindingResult result) {
    if (result.hasErrors()) {
        List<FieldError> fieldEist = result.getFieldErrors();
        if (fieldEist != null && fieldEist.size() > 0) {
            FieldError fieldError = fieldEist.get(0);
            fieldError.getCode();/*ww w . ja va 2 s  .c  om*/
            String prefix = "\r\n" + "???";
            String value = "{" + (fieldError.getRejectedValue() == null ? " " : fieldError.getRejectedValue())
                    + "}";
            String message = "" + fieldError.getDefaultMessage();
            return prefix + "" + value + "\r\n" + message;
        } else {
            List<ObjectError> allErrorlist = result.getAllErrors();
            ObjectError oe = allErrorlist.get(0);
            return "" + oe.getDefaultMessage();
        }
    } else {
        return null;
    }
}

From source file:com.google.ie.dto.ViewStatus.java

/**
 * @param errors/*from  w  ww  . j a  va  2s  .  com*/
 */
public static ViewStatus createErrorViewStatus(BindingResult errors) {
    ViewStatus viewStatus = new ViewStatus();
    log.warn("Idea object has " + errors.getErrorCount() + " validation errors");
    viewStatus.setStatus(WebConstants.ERROR);
    for (Iterator<FieldError> iterator = errors.getFieldErrors().iterator(); iterator.hasNext();) {
        FieldError fieldError = iterator.next();
        viewStatus.addMessage(fieldError.getField(), fieldError.getDefaultMessage());
        log.warn("Error found in field: " + fieldError.getField() + " Message :"
                + fieldError.getDefaultMessage());
    }
    return viewStatus;

}

From source file:org.wallride.web.support.RestValidationErrorModel.java

public static RestValidationErrorModel fromBindingResult(BindingResult result,
        MessageSourceAccessor messageSourceAccessor) {
    RestValidationErrorModel restResult = new RestValidationErrorModel();
    restResult.globalErrors = new ArrayList<>();
    for (ObjectError error : result.getGlobalErrors()) {
        restResult.globalErrors.add(/*from  www . j  av  a  2 s .  c  om*/
                messageSourceAccessor.getMessage(error.getDefaultMessage(), LocaleContextHolder.getLocale()));
    }
    restResult.fieldErrors = new LinkedHashMap<>();
    for (FieldError error : result.getFieldErrors()) {
        restResult.fieldErrors.put(error.getField(),
                messageSourceAccessor.getMessage(error, LocaleContextHolder.getLocale()));
    }
    return restResult;
}

From source file:com.tsguild.videogamewebapp.validation.RestValidationHandler.java

@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)//  w w  w  .j  a v  a2s . c  o  m
@ResponseBody
public ValidationErrorContainer processValidationErrors(MethodArgumentNotValidException e) {
    BindingResult result = e.getBindingResult();
    List<FieldError> fieldErrors = result.getFieldErrors();

    ValidationErrorContainer errors = new ValidationErrorContainer();
    fieldErrors.stream().forEach((currentError) -> {
        errors.addValidationError(currentError.getField(), currentError.getDefaultMessage());
    });
    return errors;
}

From source file:com.thesoftwareguild.addressbook.validation.RestValidationHandler.java

@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)/*from   ww w  .  j a v a  2s.c o m*/
@ResponseBody
public ValidationErrorContainer processValidationErrors(MethodArgumentNotValidException e) {

    BindingResult result = e.getBindingResult();
    List<FieldError> fieldErrors = result.getFieldErrors();
    ValidationErrorContainer errors = new ValidationErrorContainer();

    for (FieldError currentError : fieldErrors) {
        errors.addValidationError(currentError.getField(), currentError.getDefaultMessage());
    }
    return errors;
}

From source file:com.tsg.contactlistmvc.validation.RestValidationHandler.java

@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)/*  w w w .  ja  v  a  2 s  .co m*/
@ResponseBody
public ValidationErrorContainer processValidationErrors(MethodArgumentNotValidException e) {
    BindingResult result = e.getBindingResult();
    List<FieldError> fieldErrors = result.getFieldErrors();

    ValidationErrorContainer errors = new ValidationErrorContainer();
    for (FieldError currentError : fieldErrors) {
        errors.addValidationError(currentError.getField(), currentError.getDefaultMessage());

    }
    return errors;
}

From source file:com.tsguild.dolphinblog.controller.RestValidationHandler.java

@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseBody/*from www.  j  a v  a2  s .c om*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ValidationErrorContainer processValidationErrors(MethodArgumentNotValidException e) {
    BindingResult result = e.getBindingResult();
    List<FieldError> fieldErrors = result.getFieldErrors();

    ValidationErrorContainer errors = new ValidationErrorContainer();
    for (FieldError fError : fieldErrors) {
        errors.addValidationError(fError.getField(), fError.getDefaultMessage());
    }

    return errors;
}

From source file:com.swcguild.addressbookmvc.validation.RestValidationHandler.java

@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)/*from   w  w  w  . j a  va 2  s.c o m*/
@ResponseBody
public ValidationErrorContainer processValidationErrors(MethodArgumentNotValidException e) {

    BindingResult result = e.getBindingResult();
    List<FieldError> fieldErrors = result.getFieldErrors();

    ValidationErrorContainer errors = new ValidationErrorContainer();

    for (FieldError currentError : fieldErrors) {
        errors.addValidationError(currentError.getField(), currentError.getDefaultMessage());
    }
    return errors;
}

From source file:com.sg.addressbookmvc.validation.RestValidationHandler.java

@ExceptionHandler(MethodArgumentNotValidException.class)

@ResponseStatus(HttpStatus.BAD_REQUEST)// ww  w .  ja  v  a  2 s. c o  m

@ResponseBody
public ValidationErrorContainer processValidationErrors(MethodArgumentNotValidException e) {

    BindingResult result = e.getBindingResult();
    List<FieldError> fieldErrors = result.getFieldErrors();

    ValidationErrorContainer errors = new ValidationErrorContainer();
    for (FieldError currentError : fieldErrors) {
        errors.addValidationError(currentError.getField(), currentError.getDefaultMessage());
    }
    return errors;
}

From source file:com.swcguild.blacksmithblogcapstone.validation.RestValidationHandler.java

@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ResponseBody//from   w  w  w.  j av  a  2 s .c om
public ValidationErrorContainer processValidationErrors(MethodArgumentNotValidException e) {

    BindingResult result = e.getBindingResult();
    List<FieldError> fieldErrors = result.getFieldErrors();

    ValidationErrorContainer errors = new ValidationErrorContainer();
    for (FieldError currentError : fieldErrors) {
        errors.addValidationError(currentError.getField(), currentError.getDefaultMessage());
    }

    return errors;

}