I want to use both InCell editing in DataTable and the validation. I know that the trivial validations can be solved with f:validator, but what with non-trivial names?
I must, let’s say, assure that the ‘name’ property is unique in the table. So, before accepting the edit, I should check if the name is changed and if it is used by another element. If so, the edit must be refused.
How to achieve it? The eventListener would be only notified that the edit was accepted, as I have understood, so theoretically I could react and revert it, but I would prefer to refuse the edit when user clicks the ‘accept’ icon.
Like Daniel said, you can use a JSF validator for this. A short example:
Assume we have a person:
And a very simple backing bean:
For example we want to ensure the first name starts with an uppercase letter. We don’t care if the last name doesn’t start with an uppercase letter (because of compatibility with IE or a legacy database, you know, the usual weirdness).
Now to display everything nicely:
If you’re interested, validation can be configured on domain-level by using bean validation (JSR-303). I strongly recommend it since it doesn’t depend on JSF and it integrates with JPA.
Update as promised using bean validation:
First, the validator:
The annotation we’re going to use:
Note that the message we declared
"{FirstUpper.message}"will be resolved to a resource bundle. The bundle must be in the root of your classpath, namedValidationMessages.properties. To localize you can add a locale code:ValidationMessages_en.properties.In that file declare the message:
The person class:
And now, in your UI you don’t have to refer to the validator, JSF is smart enough to validate using JSR-303. So instead of this:
Just use this:
Easy right? 😉