Search in sources :

Example 76 with ObjectError

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);
    }
}
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 77 with ObjectError

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();
}
Also used : ObjectError(org.springframework.validation.ObjectError) FieldError(org.springframework.validation.FieldError)

Example 78 with ObjectError

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;
}
Also used : BindValidationException(org.springframework.boot.context.properties.bind.validation.BindValidationException) ObjectError(org.springframework.validation.ObjectError) BindException(org.springframework.boot.context.properties.bind.BindException)

Example 79 with ObjectError

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));
}
Also used : BindingResult(org.springframework.validation.BindingResult) MapBindingResult(org.springframework.validation.MapBindingResult) ObjectError(org.springframework.validation.ObjectError) BindException(org.springframework.validation.BindException) MapBindingResult(org.springframework.validation.MapBindingResult) ServletException(jakarta.servlet.ServletException) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) BindException(org.springframework.validation.BindException) Test(org.junit.jupiter.api.Test)

Example 80 with ObjectError

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));
}
Also used : BindingResult(org.springframework.validation.BindingResult) MapBindingResult(org.springframework.validation.MapBindingResult) ObjectError(org.springframework.validation.ObjectError) MapBindingResult(org.springframework.validation.MapBindingResult) Method(java.lang.reflect.Method) MethodParameter(org.springframework.core.MethodParameter) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) ServletException(jakarta.servlet.ServletException) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) BindException(org.springframework.validation.BindException) Test(org.junit.jupiter.api.Test)

Aggregations

ObjectError (org.springframework.validation.ObjectError)106 FieldError (org.springframework.validation.FieldError)19 BindingResult (org.springframework.validation.BindingResult)17 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)17 BindException (org.springframework.validation.BindException)14 ArrayList (java.util.ArrayList)13 MapBindingResult (org.springframework.validation.MapBindingResult)13 Test (org.junit.jupiter.api.Test)12 ModelAndView (org.springframework.web.servlet.ModelAndView)10 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)7 Test (org.junit.Test)6 BusinessRuleException (org.mifos.service.BusinessRuleException)6 Errors (org.springframework.validation.Errors)6 MethodArgumentNotValidException (org.springframework.web.bind.MethodArgumentNotValidException)6 DocumentBuilder (javax.xml.parsers.DocumentBuilder)5 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)5 OnmsLocationMonitor (org.opennms.netmgt.model.OnmsLocationMonitor)5 LocationMonitorIdCommand (org.opennms.web.svclayer.model.LocationMonitorIdCommand)5 Document (org.w3c.dom.Document)5 Element (org.w3c.dom.Element)5