use of org.springframework.validation.ObjectError 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.ObjectError 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.ObjectError in project spring-boot by spring-projects.
the class BindValidationFailureAnalyzer method getBindValidationExceptionDetails.
private ExceptionDetails getBindValidationExceptionDetails(Throwable rootFailure) {
BindValidationException validationException = findCause(rootFailure, BindValidationException.class);
if (validationException != null) {
BindException target = findCause(rootFailure, BindException.class);
List<ObjectError> errors = validationException.getValidationErrors().getAllErrors();
return new ExceptionDetails(errors, target, validationException);
}
org.springframework.validation.BindException bindException = findCause(rootFailure, org.springframework.validation.BindException.class);
if (bindException != null) {
List<ObjectError> errors = bindException.getAllErrors();
return new ExceptionDetails(errors, bindException.getTarget(), bindException);
}
return null;
}
use of org.springframework.validation.ObjectError in project spring-boot by spring-projects.
the class DefaultErrorAttributesTests method withBindingErrors.
@Test
void withBindingErrors() {
BindingResult bindingResult = new MapBindingResult(Collections.singletonMap("a", "b"), "objectName");
bindingResult.addError(new ObjectError("c", "d"));
Exception ex = new BindException(bindingResult);
testBindingResult(bindingResult, ex, ErrorAttributeOptions.of(Include.MESSAGE, Include.BINDING_ERRORS));
}
use of org.springframework.validation.ObjectError in project spring-boot by spring-projects.
the class DefaultErrorAttributesTests method withMethodArgumentNotValidExceptionBindingErrors.
@Test
void withMethodArgumentNotValidExceptionBindingErrors() {
Method method = ReflectionUtils.findMethod(String.class, "substring", int.class);
MethodParameter parameter = new MethodParameter(method, 0);
BindingResult bindingResult = new MapBindingResult(Collections.singletonMap("a", "b"), "objectName");
bindingResult.addError(new ObjectError("c", "d"));
Exception ex = new MethodArgumentNotValidException(parameter, bindingResult);
testBindingResult(bindingResult, ex, ErrorAttributeOptions.of(Include.MESSAGE, Include.BINDING_ERRORS));
}
Aggregations