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:se.skltp.cooperation.web.rest.exception.DefaultExceptionHandler.java

@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)/*from   w w w.  j a v  a  2  s  . c  o m*/
@ResponseBody
public ProblemDetail processValidationError(HttpServletRequest request, MethodArgumentNotValidException e) {
    log.debug("Handling form validation error");

    ValidationError error = new ValidationError();
    buildErrorMessage(request, e, HttpStatus.BAD_REQUEST, error);

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

    for (FieldError fieldError : fieldErrors) {
        log.debug("Adding error message: {} to field: {}", fieldError.getDefaultMessage(),
                fieldError.getField());
        error.addFieldError(fieldError.getField(), fieldError.getCode(), fieldError.getDefaultMessage());
    }

    return error;
}

From source file:com.springsource.greenhouse.signup.SignupController.java

private List<Map<String, String>> getErrorsMap(BindingResult formBinding) {
    List<FieldError> fieldErrors = formBinding.getFieldErrors();
    List<Map<String, String>> errors = new ArrayList<Map<String, String>>(fieldErrors.size());
    for (FieldError fieldError : fieldErrors) {
        Map<String, String> fieldErrorMap = new HashMap<String, String>();
        fieldErrorMap.put("field", fieldError.getField());
        fieldErrorMap.put("code", fieldError.getCode());
        fieldErrorMap.put("message", fieldError.getDefaultMessage());
        errors.add(fieldErrorMap);/*from w  w w . ja  v  a2 s.  c om*/
    }
    return errors;
}

From source file:com.baidu.stqa.signet.web.common.exception.ExceptionResolver.java

public Object getDecorateErrors(Exception ex) {
    HashMap<String, String> errors = new HashMap<String, String>();

    BindingResult bindingResult = ((MethodArgumentNotValidException) ex).getBindingResult();
    List<FieldError> fieldErrors = bindingResult.getFieldErrors();
    for (FieldError error : fieldErrors) {
        errors.put(error.getField(), error.getDefaultMessage());
    }//from w w  w  .  j a va2  s.  co  m
    return errors;
}

From source file:org.apigw.authserver.web.admin.DefaultAdminExceptionHandler.java

@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)/*from  ww  w.j  av a  2  s .c o m*/
@ResponseBody
public ValidationErrorDTO processValidationError(MethodArgumentNotValidException e) {
    log.debug("Handling form validation error");

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

    return processFieldErrors(fieldErrors);
}

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

/**
 * ???/*ww  w . j a va 2 s .c  o m*/
 * 
 * @param result
 * @return
 */
public String showFirstErrors(BindingResult result) {
    if (result.hasErrors()) {
        for (FieldError error : result.getFieldErrors()) {
            return error.getDefaultMessage();
        }
    }
    return StringUtils.EMPTY;
}

From source file:com.zq.fin.seckill.web.BaseController.java

 protected void validate(BindingResult result, Model model){
   ObjectError gloerror = result.getGlobalError();
   List<FieldError> errors= result.getFieldErrors();
      //from  w w w.j  ava2  s .  co m
   if(ObjectUtil.isNotEmpty(gloerror)){
      model.addAttribute("glo", gloerror.getDefaultMessage());
   }
   for(FieldError fieldError:errors){
      model.addAttribute(fieldError.getField(), fieldError.getDefaultMessage());
   }
}

From source file:com.jjcosare.calculator.controller.CalculatorController.java

@RequestMapping(value = "/", method = RequestMethod.GET)
public @ResponseBody Object getCalculation(@Valid CalculatorForm calculatorForm, BindingResult bindingResult) {

    Object result = null;/*from   w  w  w .  java 2s .  c o m*/
    if (bindingResult.hasErrors()) {
        Map<String, String> errorMap = new HashMap<>();
        for (FieldError error : bindingResult.getFieldErrors()) {
            errorMap.put(error.getField(), error.getDefaultMessage());
        }
        result = errorMap;
    } else {
        result = calculatorService.getCalculation(calculatorForm);
    }
    return result;
}

From source file:com.github.javarch.jsf.context.MessageContext.java

public void addError(BindingResult erros) {
    Assert.notNull(erros, "BindingResult no pode ser nulo.");

    for (FieldError fieldError : erros.getFieldErrors()) {
        addError(fieldError.getDefaultMessage());
    }/*from   w w w.  jav  a  2  s  .c  om*/
}

From source file:cz.jirutka.spring.exhandler.handlers.MethodArgumentNotValidExceptionHandler.java

@Override
public ValidationErrorMessage createBody(MethodArgumentNotValidException ex, HttpServletRequest req) {

    ErrorMessage tmpl = super.createBody(ex, req);
    ValidationErrorMessage msg = new ValidationErrorMessage(tmpl);

    BindingResult result = ex.getBindingResult();

    for (ObjectError err : result.getGlobalErrors()) {
        msg.addError(err.getDefaultMessage());
    }// ww  w .  j  a  va 2s  .c  o m
    for (FieldError err : result.getFieldErrors()) {
        msg.addError(err.getField(), err.getRejectedValue(), err.getDefaultMessage());
    }
    return msg;
}

From source file:com.tkmtwo.exhandler.handlers.BindExceptionHandler.java

@Override
public ValidationErrorMessage createBody(BindException ex, HttpServletRequest req) {

    ErrorMessage tmpl = super.createBody(ex, req);
    ValidationErrorMessage msg = new ValidationErrorMessage(tmpl);

    BindingResult result = ex.getBindingResult();

    for (ObjectError err : result.getGlobalErrors()) {
        msg.addError(err.getDefaultMessage());
    }/* ww w.jav a 2  s .  c  o m*/
    for (FieldError err : result.getFieldErrors()) {
        msg.addError(err.getField(), err.getRejectedValue(), err.getDefaultMessage());
    }
    return msg;
}