You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@myfaces.apache.org by Jurgen Lust <Ju...@UGent.be> on 2006/03/11 16:51:00 UTC

hibernate validator

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

Re: hibernate validator

Posted by Jurgen Lust <Ju...@UGent.be>.
Hmm, I hadn't thought about the value change events yet. That would be a 
problem. I first had the idea to plugin a custom LifeCycleImpl, that 
does not invoke the validator of each component, but instead calls the 
hibernate validator on each property, but the problem there is that 
Hibernate Validator is best used to validate an entire bean in one go 
(or at least, that's how I understand it).

Jurgen

Mario Ivankovits schreef:
> Hi!
>
> I do this in FacesFreeway too.
>
> But here I also generate the required components and thus I can attach
> any validator I want.
> To have it cleanly integrated in the phases of jsf we have to instrument
> the tree after it has been created (yes martin, I know this is a no-no
> for the standard ;-) )
>
> But you can create your own view-handler and process all UIInputs after
> createView parse the binding get its annotations and attach an
> appropiated validator.
> This wont work with components e.g. within an table or any other
> components where the reference of the binding is only reachable during
> the encoding phase.
> Its more often the case than you might think ;-)
>
> Doing this after the validation phase - during update model is a hack,
> isnt it? e.g. it will fire valueChanges where the value to be changed to
> might be and invalid value, no?
>
> Ciao,
> Mario
>   
>> There might be some overlap here with Seam-- we've already implemented
>> some of this.
>>
>> Sylvain Vieujot wrote:
>>
>>     
>>> I agree, it would be very nice and avoid double validation code for
>>> the hibernate users.
>>> It would also prevent meaning less errors for the users and show the
>>> exact problem.
>>>
>>> Great idea !
>>>
>>> Sylvain.
>>>
>>> On Sat, 2006-03-11 at 17:45 +0100, Jurgen Lust wrote:
>>>
>>>       
>>>> 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
>>>>>           
>>>>         
>>     
>
>
>   


Re: hibernate validator

Posted by Mario Ivankovits <ma...@ops.co.at>.
Hi!

I do this in FacesFreeway too.

But here I also generate the required components and thus I can attach
any validator I want.
To have it cleanly integrated in the phases of jsf we have to instrument
the tree after it has been created (yes martin, I know this is a no-no
for the standard ;-) )

But you can create your own view-handler and process all UIInputs after
createView parse the binding get its annotations and attach an
appropiated validator.
This wont work with components e.g. within an table or any other
components where the reference of the binding is only reachable during
the encoding phase.
Its more often the case than you might think ;-)

Doing this after the validation phase - during update model is a hack,
isnt it? e.g. it will fire valueChanges where the value to be changed to
might be and invalid value, no?

Ciao,
Mario
> There might be some overlap here with Seam-- we've already implemented
> some of this.
>
> Sylvain Vieujot wrote:
>
>> I agree, it would be very nice and avoid double validation code for
>> the hibernate users.
>> It would also prevent meaning less errors for the users and show the
>> exact problem.
>>
>> Great idea !
>>
>> Sylvain.
>>
>> On Sat, 2006-03-11 at 17:45 +0100, Jurgen Lust wrote:
>>
>>> 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
>>>
>>>
>
>


-- 
mfg

Mario Ivankovits - OPS EDV Vertriebsges.m.b.H.
Software Engineering
Michael-Bernhard-Gasse 10, A-1120 Wien
Tel.: +43-1-8938810
Fax: +43-1-8938810/3700
E-Mail: mario@ops.co.at
Skype: mario_ivankovits


Re: hibernate validator

Posted by Jurgen Lust <Ju...@UGent.be>.
Yes, I've been reading a bit about the Seam framework, it looks 
extremely promising. However, I need a solution that I can plug in to 
our existing Hibernate-Spring-JSF framework, and I don't think 
introducing Seam is an option right now. But I'll definitely keep my eye 
on it, it has some very nice features...

Jurgen

Jacob Hookom schreef:
> There might be some overlap here with Seam-- we've already implemented 
> some of this.
>
> Sylvain Vieujot wrote:
>
>> I agree, it would be very nice and avoid double validation code for 
>> the hibernate users.
>> It would also prevent meaning less errors for the users and show the 
>> exact problem.
>>
>> Great idea !
>>
>> Sylvain.
>>
>> On Sat, 2006-03-11 at 17:45 +0100, Jurgen Lust wrote:
>>
>>> 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
>>>
>>>
>
>


Re: hibernate validator

Posted by Jacob Hookom <ja...@hookom.net>.
There might be some overlap here with Seam-- we've already implemented 
some of this.

Sylvain Vieujot wrote:

> I agree, it would be very nice and avoid double validation code for 
> the hibernate users.
> It would also prevent meaning less errors for the users and show the 
> exact problem.
>
> Great idea !
>
> Sylvain.
>
> On Sat, 2006-03-11 at 17:45 +0100, Jurgen Lust wrote:
>
>> 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
>>
>>


-- 
Jacob Hookom  -  Minneapolis
----------------------------
JSF-EG, JSF-RI, EL, Facelets


Re: hibernate validator

Posted by Sylvain Vieujot <sv...@apache.org>.
I agree, it would be very nice and avoid double validation code for the
hibernate users.
It would also prevent meaning less errors for the users and show the
exact problem.

Great idea !

Sylvain.

On Sat, 2006-03-11 at 17:45 +0100, Jurgen Lust wrote:

> 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 
> 
> 

Re: hibernate validator

Posted by Jurgen Lust <Ju...@UGent.be>.
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