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.swcguild.blacksmithblogcapstone.validation.RestValidationHandler.java

@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ResponseBody//from   w ww.java2s .c  o m
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.cisco.cta.taxii.adapter.settings.BindExceptionHandler.java

@Override
public void handle(BindException e) {
    for (ObjectError error : e.getAllErrors()) {
        StringBuilder b = new StringBuilder().append("Error in application.yml").append(", section ")
                .append(error.getObjectName());
        if (error instanceof FieldError) {
            FieldError fieldError = (FieldError) error;
            b.append(", ").append(fieldError.getField()).append(" has illegal value ")
                    .append(fieldError.getRejectedValue());
        }// ww  w.ja v a2  s. co  m
        err.println(b.toString());
    }
}

From source file:com.thesoftwareguild.flightmaster.validation.RestValidaitonHandler.java

@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)/* w  w 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.jjcosare.calculator.controller.CalculatorController.java

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

    Object result = null;/*w  w w .j  av a2s.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.seabee.snapdragon.validation.RestValidationHandler.java

@ExceptionHandler(MethodArgumentNotValidException.class)
// #3: Specify the HTTP Status code to return when an error occurs
@ResponseStatus(HttpStatus.BAD_REQUEST)/*  w  w  w . j a va 2 s . c  o  m*/
// #4: Let Spring know that we have something to return in the body of the
// response. In this case it will be a ValidationErrorContainer containing
// a ValidationError object for each field that did not pass validation.
@ResponseBody
public ValidationErrorContainer processValidationErrors(MethodArgumentNotValidException e) {
    // #5: get the Binding Result and all field errors
    BindingResult result = e.getBindingResult();
    List<FieldError> fieldErrors = result.getFieldErrors();

    // #6: Create a new ValidationError for each fieldError in the Binding Result
    ValidationErrorContainer errors = new ValidationErrorContainer();
    for (FieldError currentError : fieldErrors) {
        errors.addValidationError(currentError.getField(), currentError.getDefaultMessage());
    }
    return errors;
}

From source file:business.CustomResponseEntityExceptionHandler.java

@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex,
        HttpHeaders headers, HttpStatus status, WebRequest request) {
    List<FieldError> fieldErrors = ex.getBindingResult().getFieldErrors();
    List<ObjectError> globalErrors = ex.getBindingResult().getGlobalErrors();
    List<String> errors = new ArrayList<>(fieldErrors.size() + globalErrors.size());
    for (FieldError fieldError : fieldErrors) {
        errors.add(fieldError.getField() + ", " + fieldError.getDefaultMessage());
    }/*ww  w  .j  a  v a 2 s.c  o m*/
    for (ObjectError objectError : globalErrors) {
        errors.add(objectError.getObjectName() + ", " + objectError.getDefaultMessage());
    }
    ErrorMessage errorMessage = new ErrorMessage(errors);
    return new ResponseEntity<Object>(errorMessage, headers, status);
}

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

@ExceptionHandler(MethodArgumentNotValidException.class)

// #3: Specify the Http Status code to return when an error occurs 
@ResponseStatus(HttpStatus.BAD_REQUEST)//from  w  w  w . j  a  v  a2 s .c  om

// #4: Let Spring know that we have something to return in the body of the
//     response.  In this case it will be a ValidationErrorContainer containing
// a ValidationError object for each field that did not pass validation. 
@ResponseBody
public ValidationErrorContainer processValidationErrors(MethodArgumentNotValidException e) {
    // #5: get the Binding Result and all field errors 
    BindingResult result = e.getBindingResult();
    List<FieldError> fieldErrors = result.getFieldErrors();

    // #6: Create a new ValidationError for each fieldError in the Binding Result
    ValidationErrorContainer errors = new ValidationErrorContainer();
    for (FieldError currentError : fieldErrors) {
        errors.addValidationError(currentError.getField(), currentError.getDefaultMessage());
    }
    return errors;
}

From source file:com.tsg.addressbookmvc.validation.RESTValidationHandler.java

@ExceptionHandler(MethodArgumentNotValidException.class)
// #3: Specify the HTTP Status code to return when an error occurs 
@ResponseStatus(HttpStatus.BAD_REQUEST)/*from ww  w. jav  a 2  s.c  o  m*/
// #4: Let Spring know that we have something to return in the body of the
// response. In this case it will be a ValidationErrorContainer containing
// a ValidationError object for each field that did not pass validation. 
@ResponseBody
public ValidationErrorContainer processValidationErrors(MethodArgumentNotValidException e) {
    // #5: get the Binding Result and all field errors 
    BindingResult result = e.getBindingResult();
    List<FieldError> fieldErrors = result.getFieldErrors();

    // #6: Create a new ValidationError for each fieldError in the Binding Result 
    ValidationErrorContainer errors = new ValidationErrorContainer();
    for (FieldError currentError : fieldErrors) {
        errors.addValidationError(currentError.getField(), currentError.getDefaultMessage());

    }
    return errors;
}

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

@ExceptionHandler(MethodArgumentNotValidException.class)
// #3: Specify the Http Status code to return when an error occurs
@ResponseStatus(HttpStatus.BAD_REQUEST)/*from  ww  w.ja  v  a2 s .  c om*/
// #4: Let Spring know that we have something to return in the body of the
// response. In this case it will be a ValidationErrorContainer containing
// a ValidationError object for each field that did not pass validation.
@ResponseBody
public ValidationErrorContainer processValidationErrors(MethodArgumentNotValidException e) {
    // #5: get the Binding Result and all field errors
    BindingResult result = e.getBindingResult();
    List<FieldError> fieldErrors = result.getFieldErrors();

    // #6: Create a new ValidationError for each fieldError in the Binding Result
    ValidationErrorContainer errors = new ValidationErrorContainer();
    for (FieldError currentError : fieldErrors) {
        errors.addValidationError(currentError.getField(), currentError.getDefaultMessage());
    }
    return errors;
}

From source file:com.intel.databackend.handlers.ErrorHandler.java

@ExceptionHandler(BindException.class)
public ResponseEntity handleError(BindException ex) {
    Map<String, String> errors = new HashMap<>();
    for (FieldError error : ex.getFieldErrors()) {
        errors.put(error.getField(), error.getDefaultMessage());
    }/*  ww  w  .jav  a2s  .c  o m*/
    logger.error("Validation error: ", errors);
    return new ResponseEntity<Map<String, String>>(errors, HttpStatus.BAD_REQUEST);
}