Wiki
Clone wikiOracle JSF Expert 1Z0-896 / Bean validation
Bean validation
JSR-303 Bean validation annotations (javax.validation.constraints)
@AssertFalse
The value of the field or property must be false.
@AssertFalse boolean isUnsupported;
@AssertTrue
The value of the field or property must be true.
@AssertTrue boolean isActive;
@DecimalMax
The value of the field or property must be a decimal value lower than or equal to the number in the value element.
@DecimalMax("30.00") BigDecimal discount;
@DecimalMin
The value of the field or property must be a decimal value greater than or equal to the number in the value element.
@DecimalMin("5.00") BigDecimal discount;
@Digits
The value of the field or property must be a number within a specified range. The integer element specifies the maximum integral digits for the number, and the fraction element specifies the maximum fractional digits for the number.
@Digits(integer=6, fraction=2) BigDecimal price;
@Future
The value of the field or property must be a date in the future.
@Future Date eventDate;
@Max
The value of the field or property must be an integer value lower than or equal to the number in the value element.
@Max(10) int quantity;
@Min
The value of the field or property must be an integer value greater than or equal to the number in the value element.
@Min(5) int quantity;
@NotNull
The value of the field or property must not be null.
@NotNull String username;
@Null
The value of the field or property must be null.
@Null String unusedString;
@Past
The value of the field or property must be a date in the past.
@Past Date birthday;
@Pattern
The value of the field or property must match the regular expression defined in the regexp element.
@Pattern(regexp="\\(\\d{3}\\)\\d{3}-\\d{4}") String phoneNumber;
@Size
The size of the field or property is evaluated and must match the specified boundaries. If the field or property is a String, the size of the string is evaluated. If the field or property is a Collection, the size of the Collection is evaluated. If the field or property is a Map, the size of the Map is evaluated. If the field or property is an array, the size of the array is evaluated. Use one of the optional max or min elements to specify the boundaries.
@Size(min=2, max=240) String briefMessage;
Setting a custom message
@NotNull(message="Oi this can't be null!") private String name;
NB: For this to work the javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_AS_NULL
must be set to true as a context-param
in the web.xml
NB: Tag validator messages will override messages set by bean validation eg.
@NotNull(message="Oi this can't be null!") private String name;
<h:form> <h:inputText value="#{user.name}" required="true" requiredMessage="This CANNOT be null!" /> <h:commandButton value="Submit" /> <h:messages /> </h:form>
NB: Faces standard validators will override JSR-303 validations eg.
@Named @RequestScoped public class UserValidateBean { @Size(min = 3, message = "Name must be longer than 3 characters!") @NotNull(message = "Name can't be null") private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
<h:form> <h:panelGrid columns="2"> <h:outputLabel value="Name" /> <h:inputText id="name" value="#{userValidateBean.name}"> <f:validateLength minimum="5" /> <f:ajax event="keyup" render="nameError" /> </h:inputText> <h:message for="name" id="nameError" style="color: red"/> <h:commandButton value="Submit"/> </h:panelGrid> </h:form>
Updated