How about this approach?

  1. You annotate your model classes with Hibernate Validator annotations, for example @Range(min=10, max=20)
  2. You don't put any validators in the JSPs
  3. You implement a custom PropertyResolverImpl that does the following:
    1. set the property
    2. perform the validation with HibernateValidator on the property
    3. if the value is invalid, set the property to its original value and throw an EvaluationException
    4. The JSP is rendered with a FacesMessage next to the input, containing the Hibernate Validator error message.
Advantages:
  • All validation is in 1 place, the model class, where it belongs
  • Much cleaner JSP
Disadvantages:
  • You completely bypass the JSF process validations phase, however, since the custom PropertyResolver would reset the property to its old value when a validation error occurs, this would not really be a problem.

This approach would not work at the moment, or at least until MYFACES-1157 is fixed.

Any ideas?

Jurgen



Jurgen Lust schreef:
Hi,

I've been playing around with Hibernate Annotations a bit, and noticed that there is also something like the Hibernate Validator: http://www.hibernate.org/hib_docs/annotations/reference/en/html/validator.html

This allows you to specify constraints on your model classes, using jdk 5.0 annotations. Hibernate then automatically enforces these contraints in the persistence tier of your application.

Now I was thinking that this could also be used with JSF. Instead of putting all the JSF validation stuff in the JSPs, you should be able to use those annotations  in the validate phase.
Has anyone tried this yet? Would it be possible, and are there any pitfalls?

regards,

Jurgen

Reply via email to