Results 1 to 8 of 8

Thread: Validation before bind

  1. #1
    Join Date
    Oct 2004
    Posts
    6

    Default Validation before bind

    AFAIK there is no Framework supported way to do validation before binding the request parameters to the command object.
    However this would be useful.

    Consider following situation:
    - command object with a java.util.Date property
    - date format should be dd/mm/yyyy

    I have configured a CustomDateEditor to parse such a submitted string into the Date property of my command object. If the submitted string is not correctly formatted, the exception thrown appears in my view (yuck).
    I think there is a way to show this message in a cleaner way.

    BUT, I would like to have some validator (like the commons-validator does now after the binding) that takes a look at the inputs ('is the date string in the format that the binder expects ?') before the binding occurs (similar like client-side validation, but now server-side).

    Any ideas / comments ?

  2. #2
    Join Date
    Aug 2004
    Location
    London, UK
    Posts
    339

    Default

    I don't see what advantage this gives you - how does it help?
    Darren Davison.
    Public Key: 0xE855B3EA

  3. #3
    Join Date
    Oct 2004
    Posts
    6

    Default

    The advantage would be early filtering of wrong input data.

    The, is the binder intended to do this ?

    It seems more logical to me that e.g. a commons-validator 'required' check is executed on the input before binding, so there is almost no server processing to be done (no binding, no validation after binding and only then noticing this field was required, ...).

    Ok, maybe it's just me thinking the wrong way, but then I'm glad someone puts me on the right track again ...

  4. #4
    Join Date
    Aug 2004
    Location
    London, UK
    Posts
    339

    Default

    yes, this is basically what the Spring validation framework and binding effectively gives you. Controllers generally don't start processing until this has happened. If you're round-tripping the request to the server anyway I really don't think you'd see any performance improvements at all.

    Note that I think you can use source level meta data to generate JavaScript validation code that may help you, but I'm not personally familiar with it.

    Regards,
    Darren Davison.
    Public Key: 0xE855B3EA

  5. #5
    Join Date
    Aug 2004
    Posts
    109

    Default

    I agree with no advantage of early filtering. You should do it on the client side. As long as you let the request come to the server side let controller deal with it the way it needs to deal with it.

    As for JavaScript check Matt Raible's integration with apache commons validation framework. It used to be in the sandbox, I am not sure of its whereabouts now.
    Thanks,
    Alex.

  6. #6
    Join Date
    Oct 2004
    Posts
    6

    Default

    Ok, I can follow the reasoning ... only thing is: 'company policy' is to avoid javascript at more or less all cost (not that I agree with it fully).

  7. #7
    Join Date
    Aug 2004
    Location
    Toulouse, France
    Posts
    148

    Default

    We had this issue too and sort of solved it using a little hack.
    You can have your property editor not throwing an exception but setting to null the returned value, and then on the onBind or eventually onBindAndValidate template method, you can compare the field value bind sooner and the request to see if any value were set. If command.field == null and request.getAttribute("field") != null, roughly, you can make a rejectValue on the Errors instance.
    Hope it helps

    Olivier

  8. #8
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    2,768

    Default

    For a cleaner way to error messages, see org.springframework.validation.DefaultMessageCodes Resolver JavaDocs.

Similar Threads

  1. Replies: 2
    Last Post: Oct 10th, 2005, 05:12 PM
  2. Replies: 2
    Last Post: Apr 29th, 2005, 03:43 AM
  3. Replies: 2
    Last Post: Apr 28th, 2005, 02:01 PM
  4. Replies: 5
    Last Post: Apr 27th, 2005, 07:04 PM
  5. Domain Object Validation
    By lhilden in forum Architecture
    Replies: 4
    Last Post: Dec 14th, 2004, 07:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •