karmapolice
karmapolice

Reputation: 336

custom bean validation for jsf form?

I have implemented a custom bean validator, with its custom annotation, for an entity, to code some non-trivial multiple-field validation logic that is needed.

I also have a JSF form associated with such entity. Single-field bean validation (@NotNull, @Min, etc.) works without problems: if any field in the form is not valid, the form is not submitted and the error message appears next to the field, as specified.

But it looks like JSF does not care about the custom validator for the whole entity: the form is submitted if all the single-field validations passed. The error is caught later and the values are not saved to the database.

I tried implementing manual validation in the code that handles the submission by adding a FacesMessage and returning null in case of error, but then all the form fields are cleared, which is not very usable.

I guess I could also implement a FacesValidator that calls the JSR-303 custom bean validator, but I wonder if there is a better solution.

Upvotes: 0

Views: 26

Answers (0)

Related Questions