views:

4828

answers:

4

I'm using Spring for form input and validation. The form controller's command contains the model that's being edited. Some of the model's attributes are a custom type. For example, Person's social security number is a custom SSN type.

public class Person {
    public String getName() {...}
    public void setName(String name) {...}
    public SSN getSocialSecurtyNumber() {...}
    public void setSocialSecurtyNumber(SSN ssn) {...}
}

and wrapping Person in a Spring form edit command:

public class EditPersonCommand {
    public Person getPerson() {...}
    public void setPerson(Person person) {...}
}

Since Spring doesn't know how to convert text to a SSN, I register a customer editor with the form controller's binder:

public class EditPersonController extends SimpleFormController {
    protected void initBinder(HttpServletRequest req, ServletRequestDataBinder binder) {
        super.initBinder(req, binder);
        binder.registerCustomEditor(SSN.class, "person.ssn", new SsnEditor());
    }
}

and SsnEditor is just a custom java.beans.PropertyEditor that can convert text to a SSN object:

public class SsnEditor extends PropertyEditorSupport {
    public String getAsText() {...} // converts SSN to text
    public void setAsText(String str) {
        // converts text to SSN
        // throws IllegalArgumentException for invalid text
    }
}

If setAsText encounters text that is invalid and can't be converted to a SSN, then it throws IllegalArgumentException (per PropertyEditor setAsText's specification). The issue I'm having is that the text to object conversion (via PropertyEditor.setAsText()) takes place before my Spring validator is called. When setAsText throws IllegalArgumentException, Spring simply displays the generic error message defined in errors.properties. What I want is a specific error message that depends on the exact reason why the entered SSN is invalid. PropertyEditor.setAsText() would determine the reason. I've tried embedded the error reason text in IllegalArgumentException's text, but Spring just treats it as a generic error.

Is there a solution to this? To repeat, what I want is the specific error message generated by the PropertyEditor to surface to the error message on the Spring form. The only alternative I can think of is to store the SSN as text in the command and perform validation in the validator. The text to SSN object conversion would take place in the form's onSubmit. This is less desirable as my form (and model) has many properties and I don't want to have to create and maintain a command that has each and every model attribute as a text field.

The above is just an example, my actual code isn't Person/SSN, so there's no need to reply with "why not store SSN as text..."

A: 

This sounds similar to an issue I had with NumberFormatExceptions when the value for an integer property could not be bound if, say, a String was entered in the form. The error message on the form was a generic message for that exception.

The solution was to add my own message resource bundle to my application context and add my own error message for type mismatches on that property. Perhaps you can do something similar for IllegalArgumentExceptions on a specific field.

Mark
+4  A: 

You're trying to do validation in a binder. That's not the binder's purpose. A binder is supposed to bind request parameters to your backing object, nothing more. A property editor converts Strings to objects and vice versa - it is not designed to do anything else.

In other words, you need to consider separation of concerns - you're trying to shoehorn functionality into an object that was never meant to do anything more than convert a string into an object and vice versa.

You might consider breaking up your SSN object into multiple, validateable fields that are easily bound (String objects, basic objects like Dates, etc). This way you can use a validator after binding to verify that the SSN is correct, or you can set an error directly. With a property editor, you throw an IllegalArgumentException, Spring converts it to a type mismatch error because that's what it is - the string doesn't match the type that is expected. That's all that it is. A validator, on the other hand, can do this. You can use the spring bind tag to bind to nested fields, as long as the SSN instance is populated - it must be initialized with new() first. For instance:

<spring:bind path="ssn.firstNestedField">...</spring:bind>

If you truly want to persist on this path, however, have your property editor keep a list of errors - if it is to throw an IllegalArgumentException, add it to the list and then throw the IllegalArgumentException (catch and rethrow if needed). Because you can construct your property editor in the same thread as the binding, it will be threadsafe if you simply override the property editor default behavior - you need to find the hook it uses to do binding, and override it - do the same property editor registration you're doing now (except in the same method, so that you can keep the reference to your editor) and then at the end of the binding, you can register errors by retrieving the list from your editor if you provide a public accessor. Once the list is retrieved you can process it and add your errors accordingly.

MetroidFan2002
I was afraid of this and I'm a bit disappointed with Spring's form handling. From what I'm hearing the "correct" Spring approach is to perform validation before the binding. So this means creating a form command with string fields.
Steve Kuo
Continued from prev comment:The validator validates these fields. The controller's onSubmit would then convert the string fields to their correct type and set the value on the backing model. This is a pain because now I have to re-create each editable field as a string in the form's command.
Steve Kuo
No, you validate after you bind - binding is not supposed to validate anything - if you do want this, another alternate approach is to create some sort of errors list in your specific object and store the errors in it when you bind, and have the validator check this list...
MetroidFan2002
Your problem is that you are trying to validate specific fields inside binding an object, which is why the fields should be separate on your form object to begin with - this way, Spring can bind each field, and you can manually post process this in doBind to make it your object if needed.
MetroidFan2002
+1 with Metroid. BaseCommandController binds data from incoming request to your command object, and then calls yours Validator. Take a look @Javadoc for BaseCommandController for details (Im' brief cause I don't want to post a new fully detail reply as this one is perfectly OK :)
Olivier
How can I validate after I bind? Example: I'm trying to populate an Integer field, and I want to disallow values like "abcd" or "100.53". Null is a legal value, as this field is optional. So how do I validate the form strings after it's already been bound to a null Integer? This doesn't make sense. I'm trying to solve exactly the same problem.
Mojo
A proper property editor will throw an IllegalArgumentException when you get a "abcd" or "100.53" - you can create one quite easily, and Spring provides quite a few of them. You'll get typeMismatch errors logged for each one of these encountered. Look at Spring's CustomNumberEditor - you will want to "alloWEmpty". The typeMismatches will have to be handled by your properties file or whatever error resolving scheme you have - the binder will place errors if any occur when binding, and then the validator will place errors if any occur when validating.
MetroidFan2002
There is usually a hook to register property editors with Spring - initBinder I believe is the one with MVC controllers, there's an equivalent thing for Webflow 1 FormAction subclasses.
MetroidFan2002
Please see http://static.springframework.org/spring/docs/2.0.x/reference/validation.html for more information on Spring validation.
MetroidFan2002
A: 

As said:

What I want is the specific error message generated by the PropertyEditor to surface to the error message on the Spring form

Behind the scenes, Spring MVC uses a BindingErrorProcessor strategy for processing missing field errors, and for translating a PropertyAccessException to a FieldError. So if you want to override default Spring MVC BindingErrorProcessor strategy, you must provide a BindingErrorProcessor strategy according to:

public class CustomBindingErrorProcessor implements DefaultBindingErrorProcessor {

    public void processMissingFieldError(String missingField, BindException errors) {
        super.processMissingFieldError(missingField, errors);
    }

    public void processPropertyAccessException(PropertyAccessException accessException, BindException errors) {
        if(accessException.getCause() instanceof IllegalArgumentException)
            errors.rejectValue(accessException.getPropertyChangeEvent().getPropertyName(), "<SOME_SPECIFIC_CODE_IF_YOU_WANT>", accessException.getCause().getMessage());
        else
            defaultSpringBindingErrorProcessor.processPropertyAccessException(accessException, errors);
    }

}

In order to test, Let's do the following

protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
    binder.registerCustomEditor(SSN.class, new PropertyEditorSupport() {

        public String getAsText() {
            if(getValue() == null)
                return null;

            return ((SSN) getValue()).toString();
        }

        public void setAsText(String value) throws IllegalArgumentException {
            if(StringUtils.isBlank(value))
                return;

            boolean somethingGoesWrong = true;
            if(somethingGoesWrong)
                throw new IllegalArgumentException("Something goes wrong!");
        }

    });
}

Now our Test class

public class PersonControllerTest {

    private PersonController personController;
    private MockHttpServletRequest request;

    @BeforeMethod
    public void setUp() {
        personController = new PersonController();
        personController.setCommandName("command");
        personController.setCommandClass(Person.class);
        personController.setBindingErrorProcessor(new CustomBindingErrorProcessor());

        request = new MockHttpServletRequest();
        request.setMethod("POST");
        request.addParameter("ssn", "somethingGoesWrong");
    }

    @Test
    public void done() {
        ModelAndView mav = personController.handleRequest(request, new MockHttpServletResponse());

        BindingResult bindingResult = (BindingResult) mav.getModel().get(BindingResult.MODEL_KEY_PREFIX + "command");

        FieldError fieldError = bindingResult.getFieldError("ssn");

        Assert.assertEquals(fieldError.getMessage(), "Something goes wrong!");
    }

}

regards,

Arthur Ronald F D Garcia
A: 

I believe you could just try to put this in your message source:

typeMismatch.person.ssn=Wrong SSN format

sstendal