Announcement Announcement Module
No announcement yet.
Noob confused on bind / validate Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Noob confused on bind / validate

    Everyone tells me that I shouldn't need to use Command form objects, that I should just let Spring bind and validate to my business object POJOs, and use custom binders to map the form posts to non-string values.

    But man, I can't figure it out.

    I have a POJO with a Long field that needs to be set in a form. An empty string from the form post is valid, and the field gets set to null, which is a valid value.

    If the form field is not blank, then I need to pitch it back with a validation error if the form field isn't numeric, or has a value less than zero.

    The problem is that if I validate after binding, then values like "abcd" and "2.543" bind to a null value in the Long field, and the original form value is lost.

    If I validate before binding ... well, I can't figure out how to validate before binding!

    This has a really simple and easy-to-read solution by adding a transient String field to my POJO, bind the String to my jsp instead of the Long, and then bind _that_ to the Long value within my Validator. I can set good valid error messages in the process, and it all works.

    But I'm not supposed to have to pollute my business objects with fields that are only useful to the view.

    Tell me I'm missing something here, something that doesn't involve trying to twist my head around a custom PropertyEditor.


  • #2

    You've heard correctly. You don't have to bind to a command object (although it can make your code a little easier to follow) you can bind directly to your business POJOs. It should be relatively easy. I usually do these:

    1. Create business POJOs
    2. Create FormControllers
    3. Create Validators (Command or POJO)
    4. Wire them together in your Spring config

    	<bean name="storyOneValidator" class="myapp.validators.StoryOneValidator" />
    	<bean name="storyOneController" class="myapp.web.StoryOneFormController">
    		<property name="commandClass" value="myapp.domain.StoryOne" />
    		<property name="formView" value="storyOneForm" />
    		<property name="validator" ref="storyOneValidator" />
    Please note that I almost always keep the default commandName of command so that I don't have to always specify the command name in my form.

    I believe binding is automatic for all primitive/simple types (int,string,long,boolean,etc) and PropertyEditors are pretty much only required when its a complex object.

    Can you post a couple snippets of your code or are you just asking random questions?


    • #3
      Joshua, thanks for your attention. Let me see if I can make the point clearer ...

      If I don't use a command object, and I want to bind a form field to an Integer, then I have no opportunity to validate the input string before it gets bound.

      If the user types "abcd" in an Integer field, all I get to validate is "null".

      That's okay unless "null" is valid for the field, and I want to send back a validation error on the "abcd" input string.

      I guess what you're saying is that in this instance the right thing to do is to use a command object, and let it validate the field and control the binding to the business POJO.

      Sorry I can't post a sample.



      • #4

        I'd say that if you're not using a command object, you're probably better off extending an AbstractFormController. However, I believe your assumptions are correct. If you don't use a command object, you're probably not using a SimpleFormController and binding and validating that data would fall as an exercise you would need to perform manually -- if you need to do it.

        I am saying that if you'd like all of the nice things a SimpleFormController does, it is a lot easier to use a command object: be it a POJO business object or just a simple POJO command object that is not a business object.

        Hope that helps,