Announcement Announcement Module
No announcement yet.
onBindAndValidate vs Validator Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • onBindAndValidate vs Validator

    Is there a benefit to implementing Validator as opposed to just creating an onBindAndValidate method in the FormController?

  • #2
    Personally, I like to put all of my validation code into separate validator classes. Validation code is potentially reusable, and having it in a separate class allows me to reuse it if needed. If the validation depends on resources that are not otherwise needed in the controller then your application and/or servlet context will be more intuitive and self documenting if you wire such dependencies into a separate validator object rather than wiring them all into the controller.

    Now onto a more subjective opinion. I think validation is a separate concern from the role of the controller. Therefore, your application will tend to be cleaner if you separate all validation logic into a separate class and let the controller just worry about controlling--it's hard enough to keep business logic out of the controller without having validation logic in there too. The tendency, especially for those new to MVC, is to write monolithic controllers containing lots of business logic. This really bites later when it's time to refactor the app (e.g. to move to a different MVC package or data persistence technology).

    On the other hand, if your validation logic is very simple onBindAndValidate may be a better way to go so you don't end up with a lot of trivial classes cluttering up your object space.