Validate class level bean validation constraints in JSF
Asked Answered
W

1

5

It seems that JSF 2.0 does not call "class level constraints". Quoting from an SO answer

JSF 2.0 doesn't call class level validation constraints. From JSF validation: JSF 2 provides built-in integration with JSR-303 constraints. When you are using bean validation in your application, JSF automatically uses the constraints for beans that are referenced by UIInput values.

The answer furthermore suggests using SeamFaces to validate the class-level constraints anyways.

Unfortunately this is a non-option, since it introduces a somewhat massive dependency for just validating what should be validated anyways.

My question thus is:

How can I get JSF to validate class-level constraints?

Manual validation in the controller is tedious and a lot of repeated code, and thus an option I would like to avoid.

I have attempted to do this by annotating the Controller-Field to be validated with @Valid, which didn't help.

I guess it should be possible to either make the "Process Validations" phase do that for me or hook in something akin to a Filter after the "Update Model Values" phase, that would centrally run the model values through a Validation.

Wellbeloved answered 5/1, 2015 at 14:5 Comment(0)
R
9

Until the upcoming JSF 2.3, JSF doesn't support class level validation using a.o. @Valid. This is an eternal issue, given that the very first JSF spec issue ever addresses this.

Your resort is either using a 3rd party library which has already taken care of it, or homebrewing it based on sources of the open source library in question (taking licensing into account).

Apart from SeamFaces <s:validateForm> which you already found, there's also OmniFaces <o:validateBean>. The major difference as compared to <s:validateForm> is that it doesn't use a JSF Validator, but a JSR303 ConstraintValidator (and that you've the whole entity immediately at hands without the need to declare and annotate a bunch of fields, repeating the entity's properties.

JSF 2.3 support will come in flavor of <f:validateWholeBean> which is largely based on OmniFaces <o:validateBean>.

Rambort answered 5/1, 2015 at 19:42 Comment(2)
Impressive that JSF will be basing it implementations on your work. Thanks for the info!Vitiate
In omnifaces 3.14, the <o:validateBean /> validates custom contraints at field level?Makkah

© 2022 - 2024 — McMap. All rights reserved.