Announcement Announcement Module
Collapse
No announcement yet.
binding before validating Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • binding before validating

    I noticed that SimpleFormController class has some inherited methods like onbind() and onBindAndValidate(). Why does it doing the binding before the validation? Is it more logical to do validation first, if there is errors, then no need to do further binding. I have a command object that has some primitive type like int and long. It generates some NumberFormatException because user submit null value, which I intend to catch it in validatiion method.

    Is it a required that all fields in Command Object have to be an Object type, no primitive?

  • #2
    Why does it doing the binding before the validation?
    Binding is mapping the submitted data to your domain object. Spring will automaticaly validate type mismatch errors for you. The validation step is for validating the domain object. This can only occur after binding.

    It generates some NumberFormatException because user submit null value, which I intend to catch it in validatiion method
    Again, Spring does this automatically for you. You can tidy up the message with a message property like typeMismatch.java.util.Date=invalid date.

    Comment


    • #3
      Thanks a lot for replying. Is there any particular reason that we don't validating before binding? I don't understand the design logic here. In this case, I hope all the validation and its error message can be done and specified in one place, that's the validator class. Why Spring does this checking AUTOMATICALLY for me in the first place (type mismatch check in binding)?

      Anybody else who defines a domain/command object which has some primitive type fields? This got to be a very common practice. I look into the jpetstore example, no primitive type is used.

      Comment


      • #4
        I hope all the validation and its error message can be done and specified in one place, that's the validator class
        The validator class operates on a domain object. If the binding fails (type mismatch) you don't have a valid domain object to validate. Spring is saving you some work by doing the type validation automatically - you can specify the error message - what else do you want to customise?

        Anybody else who defines a domain/command object which has some primitive type fields?
        Yes - it works as expected.

        Comment

        Working...
        X