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:org.haftrust.verifier.validator.BankValidatorRequiredFieldsTest.java

private void validateWithMissingField(String field) {
    try {//  w  w  w .  ja va 2 s  . c o m
        validate();
    } catch (Exception e) {
        // We don't care about exceptions here
    } finally {
        assertEquals(1, errors.getErrorCount());
        FieldError fieldError = errors.getFieldError();
        assertEquals(field, fieldError.getField());
    }
}

From source file:com.swcguild.capstoneproject.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. j a  v a  2  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.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());
    }/*  w  w  w . j  av a 2  s . c om*/
    return errors;
}

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

@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)//  ww  w.  ja  va 2s . c o m
@ResponseBody
public ValidationErrorContainer processMethodArgumentNotValidException(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.github.lynxdb.server.api.http.handlers.EpPut.java

@RequestMapping(path = "", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity put(Authentication _authentication, @RequestBody @Valid List<Metric> _request,
        BindingResult _bindingResult) {/*from www .ja v  a2  s . c o  m*/

    User user = (User) _authentication.getPrincipal();

    if (_bindingResult.hasErrors()) {
        ArrayList<String> errors = new ArrayList();
        _bindingResult.getFieldErrors().forEach((FieldError t) -> {
            errors.add(t.getField() + ": " + t.getDefaultMessage());
        });
        return new ErrorResponse(mapper, HttpStatus.BAD_REQUEST, errors.toString(), null).response();
    }

    List<com.github.lynxdb.server.core.Metric> metricList = new ArrayList<>();
    _request.stream().forEach((m) -> {
        metricList.add(new com.github.lynxdb.server.core.Metric(m));
    });

    try {
        entries.insertBulk(vhosts.byId(user.getVhost()), metricList);
    } catch (Exception ex) {
        throw ex;
    }
    return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
}

From source file:com.janrain.backplane.server.InvalidRequestException.java

public InvalidRequestException(BindingResult result) {

    for (FieldError error : result.getFieldErrors()) {
        fieldErrors.put(error.getField(), error.getDefaultMessage());
    }/*from  ww w.ja v  a2  s.co m*/
}

From source file:com.garyclayburg.persistence.repository.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());
    String error;//  www  .  ja  va2  s .c  o  m
    for (FieldError fieldError : fieldErrors) {
        error = fieldError.getField() + ", " + fieldError.getDefaultMessage();
        errors.add(error);
    }
    for (ObjectError objectError : globalErrors) {
        error = objectError.getObjectName() + ", " + objectError.getDefaultMessage();
        errors.add(error);
    }
    ErrorMessage errorMessage = new ErrorMessage(errors);
    return new ResponseEntity(errorMessage, headers, status);
}

From source file:br.com.javatar.examples.handler.BindExceptionExceptionHandler.java

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

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

    for (FieldError violation : ex.getFieldErrors()) {
        String pathNode = violation.getField();

        // path is probably useful only for properties (fields)
        msg.addError(pathNode, convertToString(violation.getRejectedValue()), violation.getDefaultMessage());
    }/*from  w w w .ja  v  a2 s  .  c  o  m*/

    return msg;
}

From source file:com.ns.retailmgr.controller.exception.RestControllerExceptionHandler.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());
    String error;//  ww  w  . j  a  va 2  s.  c o  m
    for (FieldError fieldError : fieldErrors) {
        error = fieldError.getField() + ", " + fieldError.getDefaultMessage();
        errors.add(error);
    }
    for (ObjectError objectError : globalErrors) {
        error = objectError.getObjectName() + ", " + objectError.getDefaultMessage();
        errors.add(error);
    }
    RestErrorMessage RestErrorMessage = new RestErrorMessage(errors);
    return new ResponseEntity(RestErrorMessage, headers, status);
}

From source file:com.github.iexel.fontus.web.rest.GlobalControllerAdviceRest.java

private String[] fetchValidationErrorsAsStringArray(BindingResult bindingResult, Locale locale) {

    String[] errorArray = null;//from w w  w. j a  v  a2s .  c  o m

    if (bindingResult.hasErrors()) {
        List<FieldError> errors = bindingResult.getFieldErrors();
        errorArray = new String[errors.size() * 2];
        int counter = 0;

        for (FieldError error : errors) {
            errorArray[counter] = error.getField();
            errorArray[counter + 1] = messageSource.getMessage(error, locale);
            counter += 2;
        }
    }
    return errorArray;
}