Search in sources :

Example 66 with FieldError

use of org.springframework.validation.FieldError in project openmrs-core by openmrs.

the class HibernateAdministrationDAOTest method validate_shouldFailValidationIfFieldLengthsAreNotCorrect.

/**
 * @see HibernateAdministrationDAO#validate(Object,Errors)
 */
@Test
public void validate_shouldFailValidationIfFieldLengthsAreNotCorrect() {
    String errorCode = "error.exceededMaxLengthOfField";
    String[] RoleFeilds = new String[] { "role", "description" };
    Role role = new Role();
    role.setRole("too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text");
    role.setDescription("too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text");
    Errors errors = new BindException(role, "type");
    dao.validate(role, errors);
    for (String feilds : RoleFeilds) {
        FieldError fielderror = errors.getFieldError(feilds);
        Assert.assertTrue(errorCode.equals(fielderror.getCode()));
    }
}
Also used : Role(org.openmrs.Role) Errors(org.springframework.validation.Errors) BindException(org.springframework.validation.BindException) FieldError(org.springframework.validation.FieldError) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 67 with FieldError

use of org.springframework.validation.FieldError in project openmrs-core by openmrs.

the class ValidateUtil method validate.

/**
 * Test the given object against all validators that are registered as compatible with the
 * object class
 *
 * @param obj the object to validate
 * @throws ValidationException thrown if a binding exception occurs
 * @should throw APIException if errors occur during validation
 * @should return immediately if validation is disabled
 */
public static void validate(Object obj) throws ValidationException {
    if (disableValidation) {
        return;
    }
    Errors errors = new BindException(obj, "");
    Context.getAdministrationService().validate(obj, errors);
    if (errors.hasErrors()) {
        Set<String> uniqueErrorMessages = new LinkedHashSet<>();
        for (Object objerr : errors.getAllErrors()) {
            ObjectError error = (ObjectError) objerr;
            String message = Context.getMessageSourceService().getMessage(error.getCode());
            if (error instanceof FieldError) {
                message = ((FieldError) error).getField() + ": " + message;
            }
            uniqueErrorMessages.add(message);
        }
        String exceptionMessage = "'" + obj + "' failed to validate with reason: ";
        exceptionMessage += StringUtils.join(uniqueErrorMessages, ", ");
        throw new ValidationException(exceptionMessage, errors);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Errors(org.springframework.validation.Errors) ObjectError(org.springframework.validation.ObjectError) ValidationException(org.openmrs.api.ValidationException) BindException(org.springframework.validation.BindException) OpenmrsObject(org.openmrs.OpenmrsObject) FieldError(org.springframework.validation.FieldError)

Example 68 with FieldError

use of org.springframework.validation.FieldError in project nakadi by zalando.

the class ValidationProblem method buildErrorMessage.

private String buildErrorMessage() {
    final StringBuilder detailBuilder = new StringBuilder();
    for (final ObjectError error : errors.getAllErrors()) {
        if (error instanceof FieldError) {
            final String fieldName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, ((FieldError) error).getField());
            detailBuilder.append("Field \"").append(fieldName).append("\" ").append(error.getDefaultMessage()).append("\n");
        } else {
            detailBuilder.append(error.toString());
        }
    }
    return detailBuilder.toString();
}
Also used : ObjectError(org.springframework.validation.ObjectError) FieldError(org.springframework.validation.FieldError)

Example 69 with FieldError

use of org.springframework.validation.FieldError in project alien4cloud by alien4cloud.

the class RestTechnicalExceptionHandler method processValidationError.

@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public RestResponse<FieldErrorDTO[]> processValidationError(MethodArgumentNotValidException e) {
    BindingResult result = e.getBindingResult();
    List<FieldErrorDTO> errors = Lists.newArrayList();
    for (FieldError fieldError : result.getFieldErrors()) {
        errors.add(new FieldErrorDTO(fieldError.getField(), fieldError.getCode()));
    }
    return RestResponseBuilder.<FieldErrorDTO[]>builder().data(errors.toArray(new FieldErrorDTO[errors.size()])).error(RestErrorBuilder.builder(RestErrorCode.ILLEGAL_PARAMETER).message("Method argument is invalid " + e.getMessage()).build()).build();
}
Also used : BindingResult(org.springframework.validation.BindingResult) FieldError(org.springframework.validation.FieldError) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 70 with FieldError

use of org.springframework.validation.FieldError in project spring-cloud-open-service-broker by spring-cloud.

the class BaseController method handleBindingException.

private ResponseEntity<ErrorMessage> handleBindingException(Exception ex, final BindingResult result) {
    LOGGER.error("Unprocessable request received: ", ex);
    StringBuilder message = new StringBuilder("Missing required fields:");
    for (FieldError error : result.getFieldErrors()) {
        message.append(' ').append(error.getField());
    }
    return getErrorResponse(message.toString(), HttpStatus.UNPROCESSABLE_ENTITY);
}
Also used : FieldError(org.springframework.validation.FieldError)

Aggregations

FieldError (org.springframework.validation.FieldError)92 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)20 ObjectError (org.springframework.validation.ObjectError)19 Test (org.junit.jupiter.api.Test)17 BindingResult (org.springframework.validation.BindingResult)16 ApiOperation (io.swagger.annotations.ApiOperation)14 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)12 Errors (org.springframework.validation.Errors)11 ArrayList (java.util.ArrayList)7 BindException (org.springframework.validation.BindException)6 CustomResult (com.megagao.production.ssm.domain.customize.CustomResult)5 Locale (java.util.Locale)5 Test (org.junit.Test)5 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)5 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)5 JsonResult (com.odysseusinc.arachne.commons.api.v1.dto.util.JsonResult)4 HashMap (java.util.HashMap)4 UserProfileDTO (com.odysseusinc.arachne.portal.api.v1.dto.UserProfileDTO)3 LinkedHashSet (java.util.LinkedHashSet)3 MapBindingResult (org.springframework.validation.MapBindingResult)3