webtier@glassfish.java.net

Re: [webtier] Re: [JSF 2] JSR-303 class level validators

From: Joel Weight <digitaljoel_at_gmail.com>
Date: Thu, 15 Apr 2010 16:22:17 -0600

Funny you should ask, I blogged about the whole thing just this week.
http://digitaljoel.wordpress.com/2010/04/12/jsf-2-jsr-303-and-you/

<http://digitaljoel.wordpress.com/2010/04/12/jsf-2-jsr-303-and-you/>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 =
Validation.buildDefaultValidatorFactory().getValidator();
        Set<ConstraintViolation<Object>> 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, <webtier_at_javadesktop.org> 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']
>
> http://forums.java.net/jive/thread.jspa?messageID=397230
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: webtier-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: webtier-help_at_glassfish.dev.java.net
>
>