JSF Bean Validation

JSF provides validation constraints for bean model in the form of annotations. You can place that annotations on a field, method, or class of a JavaBeans component, such as a managed bean.

JSF also provides facility to create custom or user defined constraints. The built-in constraints are available in the javax.validation.constraints package and tabled in the following table.

# Built-In Bean Validation Constraints

Constraint Description Example
@NotNull It is used to set not null constraint to the value of the field or property. @NotNullString username;
@Null It is used set null constraint to the value of the field or property. @NullString unusedString;
@Size It is used to specify size of field or property. The size of the field or property is evaluated and must match the specified boundaries. Use one of the optional max or min elements to specify the boundaries. @Size(min=2, max=240)String briefMessage;
@Digits It is used to set constraint that 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;
@DecimalMin This constraint specifies that 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;
@DecimalMax It is used to specify that 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;
@Max It is used to set the value of the field or property which must be an integer value lower than or equal to the number in the value element. @Max(10)int quantity;
@Min It is used to set the value of the field or property which must be an integer value greater than or equal to the number in the value element. @Min(5)int quantity;
@Pattern It is used to set patter which must match the regular expression defined in the regexp element. @Pattern(regexp="\\(\\d{3}\\)\\d{3}-\\d{4}")String phoneNumber;
@Past It is used to set the value of the field or property which must be a date in the past. @PastDate birthday;
@Future It is used to set the value of the field or property which must be a date in the future. @FutureDate eventDate;
@AssertTrue It is used to set the value of the field or property which must be true. @AssertTrueboolean isActive;
@AssertFalse It is used to set the value of the field or property which must be false. @AssertFalseboolean isUnsupported;

# JSF Bean Validation Example

// index.xhtml


<h:form id="form">
<h:outputLabel for="username">User Name</h:outputLabel>
<h:inputText id="name-id" value="#{user.name}"/>
<h:message for="name-id" style="color: red"/>
<br/>
<h:outputLabel for="age">Enter Age</h:outputLabel>
<h:inputText id="age-id" value="#{user.age}"/>
<h:message for="age-id" style=" color: red"/>
<br/>
<h:outputLabel for="mobile">Mobile No.</h:outputLabel>
<h:inputText id="mobile-id" value="#{user.mobile}"/>
<h:message for="mobile-id" style="color: red"/>
<br/>
<h:commandButton value="OK" action="response.xhtml"></h:commandButton>
</h:form>

// User.java


import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Min;
import javax.validation.constraints.Size;
@ManagedBean
@RequestScoped
public class User{
@NotNull(message = "Name can't be empty")
String name;
@Min(18)
int age;
@NotNull(message = "Mobile can't be empty")
@Size(min = 10, max = 10, message = "Mobile must have 10 digits")
String mobile;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
}

Place the below code in your web.xml file. It is used to tell to JSF that empty value must treated as null.

// web.xml


<context-param>
<param-name>
javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL
</param-name>
<param-value>true</param-value>
</context-param>

Output:

JSF Bean validation 1