Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring 3.1.1.RELEASE Databinding: Error when validating submitted form

Since upgrading my webapplication from Spring 3.0.5 to 3.1.1 I have to face some serious errors when validating my form-beans. My previously configured validator(s) doesn't work any more like they should. The problem is that the method getFieldValue(String fieldname) from Class org.springframework.validation.Errors does not return the original binded bean value like it should (and was before).

This is what my form-bean looks like:

public class PersonBean extends BaseFormBean {

private String firstname; // getters and setter omitted...

    private String lastname; // getters and setter omitted...

    private Integer age; // getters and setter omitted...

    public PersonBean() {}

    @Override
    public void validateForm(Errors errors) {
        WebValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstname", "validator.requiredvalidator.lbl", "field required");
        WebValidationUtils.rejectIfEmptyOrWhitespace(errors, "lastname", "validator.requiredvalidator.lbl", "field required");      
        WebValidationUtils.rejectInvalidIntValue(errors, "age", "validator.std.age", "invalid age", false);
    }
}

The WebValidationUtils-class that gets invoked has some standard methods for checking bean properties. The error occurcs only on non-String values, like the property age which is of type Integer. It also happens on Collection(s).

The following snippet shows how Integer values are validated in my utils-class:

import org.springframework.validation.Errors;
...

public abstract class WebValidationUtils {
...
    public static void rejectInvalidIntValue(Errors errors, String field, String errorCode, String defaultMessage){
        Assert.notNull(errors, "Errors object must not be null");
        Object value = errors.getFieldValue(field); // returns the string value (type: java.lang.String)
                Class<?> fieldType = errors.getFieldType(field); // returns the class Integer!
        if (value == null || !value.getClass().equals(Integer.class) || ((Integer)value).intValue() <= 0){
                errors.rejectValue(field, errorCode, null, defaultMessage);
        }
    }
}

The bean itself has the correct value bound...

Do I have to configure some additonal spring beans in my context-servlet.xml do achieve the same bevahior like it was in 3.0.5?

Edit: The official Spring Doku for the method "getFieldValue(...)" says

Return the current value of the given field, either the current bean property value or a rejected update from the last binding.

So I don't have a clue why this method returns a String-value instead of the propagated bean value of type Integer...

like image 499
tim.kaufner Avatar asked Dec 06 '25 05:12

tim.kaufner


1 Answers

Seem like you have a binding error so getFieldValue() return FieldError.getFieldValue() that return the value that causes the binding error. This is the expected behavior.

You can get the value that hold the property using getRawFieldValue() instead. This method always return the value using the PropertyAccessor.

like image 50
Jose Luis Martin Avatar answered Dec 07 '25 18:12

Jose Luis Martin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!