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

  • Validator and SimpleFormController

    Just want an opinion here.

    I have a number of simple validations to perform on some trivial screens.
    They only have a few fields that require input validation.

    Would there be a downside of implementing the Validator interface within a SimpleFormController Class?
    This would eliminate the need for an additional class with only a few lines of code.

    So I would have something like

    class SomeFormController extends SimpleFormController implements Validator {

    public void validate(Object obj, Errors errors) {
    // do my form validation
    }

    }

    Any thoughts would be appreciated.

  • #2
    I guess I would ask what the benefit is of having one less class.

    To me the potential issue is that you can't use that Validator elsewhere. Well, you could, I suppose, but you might not think to given it being named SomeFormController. So this could impact scalability and maintainability, but I don't think it wouldn't work.

    Comment


    • #3
      I agree that it makes the class less reusable, and I wouldn't try to reuse the entire controller just to do validation.
      But the validator is already bound to the form for the controller, so I don't know that there is really much reuse potential anyway.

      In addition, you can always override the Validator anyway by injecting a new Validator.

      I would define the SomeFormController as the validator in the constructor as so:

      public SomeFormController {
      setValidator(this);
      }

      So upon startup, Spring could override this behavior by injecting a different Validator.

      The approach does work BTW. I just wanted opinions as to whether or not it is considered good form.

      Thanks for the reply!

      Comment

      Working...
      X