Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate class level bean validation constraints in JSF

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.

like image 874
Vogel612 Avatar asked Feb 11 '23 05:02

Vogel612


1 Answers

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

like image 139
BalusC Avatar answered Feb 20 '23 00:02

BalusC