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()));
}
}
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);
}
}
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();
}
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();
}
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);
}
Aggregations