Skip to main content

[JSF 2] JSR-303 class level validators

3 replies [Last post]
Joined: 2007-11-12

I've got field level validation working with the reference implementation of JSR-303 (hibernate-validation-4.0.2.GA). I was able to validate a field with a
@Size(min=3, max=10)

I implemented a class level validator as described in the answer posted by Nicko in this question

but the class level validator is never called. Can someone give me pointers on how to get this working?


Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2007-11-12

I've been looking at this some more and finally (yes, I'm slow) figured out why it won't work. With a little finagling I got my class level validator evaluated in the validation phase

Since JSR-303 validators validate the instance of the bean that is marked with the annotation, and the validate phase comes before the update model values phase, the class level validator won't be validating the entered values anyway since they are still in the components and not yet in the model.

So, if I have a "new password" and a "confirm new password" field, there's really no way for me to do that now other than to mix validation technologies (JSR-303 for all my field level validation and tomahawk or custom JSF validators for multi-field validations)

I thought I saw a rumor about supporting class level JSR-303 validators in a future release of JSF, is this true?


Joined: 2005-07-12

> With a little finagling I got my class level validator
> evaluated in the validation phase

Could you explain how?


Joel Weight

Funny you should ask, I blogged about the whole thing just this week.

But if
you want to skip that, here's the relevant text

So you implement the FieldMatch validator, and place it on your type. You
then deploy your fancy JSF application and test the validator. Strangely
your validator is never run. After reading more closely, you discover that
JSF does “Field Level” validation using JSR-303 validators. Well, that
should be easily enough overcome, right?

Next, you add an f:event tag to the page containing the bean to be
validated. The event is preValidate, and you call a method on your bean
containing the values. The method you call runs the validator manually.
Perfect, right? More testing reveals that the your validator returns true
every time, and even more than that, the values it’s testing are both null
(which is why they are also equal).

In essence, I added a "validate" method to my bean that contained the values
submitted by the form. I've since gotten rid of that code, but it looked
something like this

Validator validator =
Set > violations = validator.validate( this ); and then looked through the violations set and if it's empty then validation was OK, if not, then it would add faces messages for the failures. I did have another idea while I was writing the blog. I've never done much with bindings but perhaps If you had the UIComponent bound to your bean, then I believe you might be able to use the validator to validate the values of the UIComponent values, assuming the values of the bound components are updated in the Apply Request Values phase. If you are going to do that, you would probably want to create an Interface for a validation group and put your class level validator in that validation group so you aren't re-validating all the field level validations when you run your own validation. If you try the binding stuff I would be interested to know your results since I still need a solution to this issue, but have been yanked onto another, higher priority project for the next little bit. Good Luck Joel On Thu, Apr 15, 2010 at 3:38 PM, wrote: > > > > With a little finagling I got my class level validator > > evaluated in the validation phase > > > > Could you explain how? > > Thanks. > [Message sent by forum member 'abrennan'] > > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: > For additional commands, e-mail: > > [att1.html]