Let's say that when we are adding a new celebrity to our collection the first name should be mandatory. This can be easily achieved by adding a @Validate
annotation to either getter or setter method of the Celebrity
class, like this:
@Validate("required") public String getFirstName() { return firstName; }
If you try to add a celebrity without specifying a first name, you will see an appropriate error message, as the Errors
component is already incorporated into the BeanEditForm
.
You can also specify a custom error message if you do not like the default one. Try something like this:
firstName-required-message=You cannot have a celebrity without a name!
And of course you combine different validators like we did before.