Search in sources :

Example 6 with FieldError

use of org.springframework.validation.FieldError in project spring-framework by spring-projects.

the class EscapedErrorsTests method testEscapedErrors.

@Test
public void testEscapedErrors() {
    TestBean tb = new TestBean();
    tb.setName("empty &");
    Errors errors = new EscapedErrors(new BindException(tb, "tb"));
    errors.rejectValue("name", "NAME_EMPTY &", null, "message: &");
    errors.rejectValue("age", "AGE_NOT_SET <tag>", null, "message: <tag>");
    errors.rejectValue("age", "AGE_NOT_32 <tag>", null, "message: <tag>");
    errors.reject("GENERAL_ERROR \" '", null, "message: \" '");
    assertTrue("Correct errors flag", errors.hasErrors());
    assertTrue("Correct number of errors", errors.getErrorCount() == 4);
    assertTrue("Correct object name", "tb".equals(errors.getObjectName()));
    assertTrue("Correct global errors flag", errors.hasGlobalErrors());
    assertTrue("Correct number of global errors", errors.getGlobalErrorCount() == 1);
    ObjectError globalError = errors.getGlobalError();
    assertTrue("Global error message escaped", "message: &quot; &#39;".equals(globalError.getDefaultMessage()));
    assertTrue("Global error code not escaped", "GENERAL_ERROR \" '".equals(globalError.getCode()));
    ObjectError globalErrorInList = errors.getGlobalErrors().get(0);
    assertTrue("Same global error in list", globalError.getDefaultMessage().equals(globalErrorInList.getDefaultMessage()));
    ObjectError globalErrorInAllList = errors.getAllErrors().get(3);
    assertTrue("Same global error in list", globalError.getDefaultMessage().equals(globalErrorInAllList.getDefaultMessage()));
    assertTrue("Correct field errors flag", errors.hasFieldErrors());
    assertTrue("Correct number of field errors", errors.getFieldErrorCount() == 3);
    assertTrue("Correct number of field errors in list", errors.getFieldErrors().size() == 3);
    FieldError fieldError = errors.getFieldError();
    assertTrue("Field error code not escaped", "NAME_EMPTY &".equals(fieldError.getCode()));
    assertTrue("Field value escaped", "empty &amp;".equals(errors.getFieldValue("name")));
    FieldError fieldErrorInList = errors.getFieldErrors().get(0);
    assertTrue("Same field error in list", fieldError.getDefaultMessage().equals(fieldErrorInList.getDefaultMessage()));
    assertTrue("Correct name errors flag", errors.hasFieldErrors("name"));
    assertTrue("Correct number of name errors", errors.getFieldErrorCount("name") == 1);
    assertTrue("Correct number of name errors in list", errors.getFieldErrors("name").size() == 1);
    FieldError nameError = errors.getFieldError("name");
    assertTrue("Name error message escaped", "message: &amp;".equals(nameError.getDefaultMessage()));
    assertTrue("Name error code not escaped", "NAME_EMPTY &".equals(nameError.getCode()));
    assertTrue("Name value escaped", "empty &amp;".equals(errors.getFieldValue("name")));
    FieldError nameErrorInList = errors.getFieldErrors("name").get(0);
    assertTrue("Same name error in list", nameError.getDefaultMessage().equals(nameErrorInList.getDefaultMessage()));
    assertTrue("Correct age errors flag", errors.hasFieldErrors("age"));
    assertTrue("Correct number of age errors", errors.getFieldErrorCount("age") == 2);
    assertTrue("Correct number of age errors in list", errors.getFieldErrors("age").size() == 2);
    FieldError ageError = errors.getFieldError("age");
    assertTrue("Age error message escaped", "message: &lt;tag&gt;".equals(ageError.getDefaultMessage()));
    assertTrue("Age error code not escaped", "AGE_NOT_SET <tag>".equals(ageError.getCode()));
    assertTrue("Age value not escaped", (new Integer(0)).equals(errors.getFieldValue("age")));
    FieldError ageErrorInList = errors.getFieldErrors("age").get(0);
    assertTrue("Same name error in list", ageError.getDefaultMessage().equals(ageErrorInList.getDefaultMessage()));
    FieldError ageError2 = errors.getFieldErrors("age").get(1);
    assertTrue("Age error 2 message escaped", "message: &lt;tag&gt;".equals(ageError2.getDefaultMessage()));
    assertTrue("Age error 2 code not escaped", "AGE_NOT_32 <tag>".equals(ageError2.getCode()));
}
Also used : Errors(org.springframework.validation.Errors) ObjectError(org.springframework.validation.ObjectError) TestBean(org.springframework.tests.sample.beans.TestBean) BindException(org.springframework.validation.BindException) FieldError(org.springframework.validation.FieldError) Test(org.junit.Test)

Example 7 with FieldError

use of org.springframework.validation.FieldError in project disconf by knightliao.

the class ParamValidateUtils method getParamErrors.

/**
     * 从bindException中获取到参数错误类型,参数错误
     */
public static ModelAndView getParamErrors(BindException be) {
    // 构造error的映射
    Map<String, String> paramErrors = new HashMap<String, String>();
    Map<String, Object[]> paramArgusErrors = new HashMap<String, Object[]>();
    for (Object error : be.getAllErrors()) {
        if (error instanceof FieldError) {
            FieldError fe = (FieldError) error;
            String field = fe.getField();
            // 默认的message
            String message = fe.getDefaultMessage();
            try {
                contextReader.getMessage(message, fe.getArguments());
            } catch (NoSuchMessageException e) {
                // 如果有code,则从前往后遍历Code(特殊到一般),修改message为code所对应
                for (int i = fe.getCodes().length - 1; i >= 0; i--) {
                    try {
                        String code = fe.getCodes()[i];
                        String info = contextReader.getMessage(code, fe.getArguments());
                        LOG.debug(code + "\t" + info);
                        message = code;
                    } catch (NoSuchMessageException e2) {
                        LOG.debug("");
                    }
                }
            }
            // 最终的消息
            paramErrors.put(field, message);
            paramArgusErrors.put(field, fe.getArguments());
        }
    }
    return ParamValidateUtils.paramError(paramErrors, paramArgusErrors, ErrorCode.FIELD_ERROR);
}
Also used : NoSuchMessageException(org.springframework.context.NoSuchMessageException) HashMap(java.util.HashMap) FieldError(org.springframework.validation.FieldError)

Example 8 with FieldError

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

the class GrailsDomainClassValidator method validatePropertyWithConstraint.

@SuppressWarnings("rawtypes")
private void validatePropertyWithConstraint(String propertyName, Object obj, Errors errors, BeanWrapper bean, Map constrainedProperties) {
    int i = propertyName.lastIndexOf(".");
    String constrainedPropertyName;
    if (i > -1) {
        constrainedPropertyName = propertyName.substring(i + 1, propertyName.length());
    } else {
        constrainedPropertyName = propertyName;
    }
    FieldError fieldError = errors.getFieldError(constrainedPropertyName);
    if (fieldError == null) {
        ConstrainedProperty c = (ConstrainedProperty) constrainedProperties.get(constrainedPropertyName);
        c.setMessageSource(messageSource);
        c.validate(obj, bean.getPropertyValue(constrainedPropertyName), errors);
    }
}
Also used : FieldError(org.springframework.validation.FieldError) GString(groovy.lang.GString) ConstrainedProperty(grails.validation.ConstrainedProperty)

Example 9 with FieldError

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

the class AbstractConstraintTests method testConstraintMessageCodes.

protected void testConstraintMessageCodes(Constraint constraint, Object value, String[] code, Object[] args) {
    Errors errors = testConstraintFailed(constraint, value);
    FieldError fieldError = errors.getFieldError(constraint.getPropertyName());
    for (int j = 0; j < code.length; j++) {
        checkCode(fieldError, code[j]);
    }
    checkArguments(args, fieldError.getArguments());
}
Also used : Errors(org.springframework.validation.Errors) FieldError(org.springframework.validation.FieldError)

Example 10 with FieldError

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

the class AbstractConstraintTests method testConstraintMessageCode.

protected void testConstraintMessageCode(Constraint constraint, Object value, String code, Object[] args) {
    Errors errors = testConstraintFailed(constraint, value);
    FieldError fieldError = errors.getFieldError(constraint.getPropertyName());
    checkCode(fieldError, code);
    checkArguments(args, fieldError.getArguments());
}
Also used : Errors(org.springframework.validation.Errors) FieldError(org.springframework.validation.FieldError)

Aggregations

FieldError (org.springframework.validation.FieldError)25 ObjectError (org.springframework.validation.ObjectError)8 Errors (org.springframework.validation.Errors)7 Test (org.junit.Test)6 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)5 Locale (java.util.Locale)4 BindingResult (org.springframework.validation.BindingResult)4 BigDecimal (java.math.BigDecimal)2 HashMap (java.util.HashMap)2 ConverterException (org.grails.web.converters.exceptions.ConverterException)2 LocalDate (org.joda.time.LocalDate)2 MessageContext (org.springframework.binding.message.MessageContext)2 BindException (org.springframework.validation.BindException)2 DuplicateUserException (com.jappstart.exception.DuplicateUserException)1 UserAccount (com.jappstart.model.auth.UserAccount)1 GrailsApplication (grails.core.GrailsApplication)1 GrailsDomainClass (grails.core.GrailsDomainClass)1 CollectionDataBindingSource (grails.databinding.CollectionDataBindingSource)1 DataBinder (grails.databinding.DataBinder)1 DataBindingSource (grails.databinding.DataBindingSource)1