servlets struts hibernate spring maven java
 

JSF Validation <f:validateBean> Tag



It is utilized to enlist a bean validator to the segment. For validating bean model, you should set the setting parameter in the web sending descriptor file web.xml.

Bean Validation Constraints

JSF gives constraints validation to bean model as annotations. You can put that annotations on a field, technique, or class of a JavaBeans part, for example, an managed bean.

JSF additionally gives facility to make custom or user characterized imperatives. The inherent imperatives are accessible in the javax.validation.constraints bundle and postponed in the accompanying table.

Built-In Bean Validation Constraints

Constraint Description Example
@NotNull It is utilized to set not null constraint to the value of the field or property. @NotNull String username;
@Null It is utilized set null constraint to the value of the field or property. @Null String unusedString;
@Size It is utilized to indicate size of field or property. The span of the field or property is assessed and should coordinate the predetermined limits. Utilize one of the discretionary max or min components to indicate the boundaries. @Size(min=2, max=240) String briefMessage;
@Digits It is utilized to set limitation that the value of the field or property must be a number inside a predefined run. The whole number component indicates the greatest basic digits for the number, and the portion component determines the most extreme fragmentary digits for the number. @Digits(integer=6, fraction=2) BigDecimal cost;
@DecimalMin This requirement determines that the value of the field or property must be a decimal esteem more noteworthy than or equivalent to the number in the esteem element. @DecimalMin("5.00") BigDecimal markdown;
@DecimalMax It is utilized to determine that the value of the field or property must be a decimal esteem lower than or equivalent to the number in the esteem element. @DecimalMax("30.00") BigDecimal markdown;
@Max It is utilized to set the value of the field or property which must be a whole number esteem lower than or equivalent to the number in the esteem element. @Max(10) int amount;
@Min It is utilized to set the value of the field or property which must be a whole number esteem more prominent than or equivalent to the number in the esteem element. @Min(5) int amount;
@Pattern It is utilized to set patter which must match the standard articulation characterized in the regexp element. @Pattern(regexp="\\(\\d{3}\\)\\d{3}-\\d{4}") String phoneNumber;
@Past It is utilized to set the value of the field or property which must be a date in the past. @Past Date birthday;
@Future It is utilized to set the value of the field or property which must be a date in the future. @Future Date eventDate;
@AssertTrue It is utilized to set the value of the field or property which must be true. @AssertTrue boolean isActive;
@AssertFalse It is utilized to set the value of the field or property which must be false. @AssertFalse boolean isUnsupported;

// web.xml

  1. <context-param>  
  2. <param-name>  
  3. javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL  
  4. </param-name>  
  5. <param-value>true</param-value>  
  6. </context-param>  

This parameter value enables the JavaServer Faces implementation to treat empty strings as null.

// User.java

  1. import javax.faces.bean.ManagedBean;  
  2. import javax.faces.bean.RequestScoped;  
  3. import javax.validation.constraints.NotNull;  
  4. @ManagedBean  
  5. @RequestScoped  
  6. public class User{  
  7.   
  8. @NotNull(message = "Name can't be null")  
  9. String name;  
  10.   
  11. public String getName() {  
  12. return name;  
  13. }  
  14. public void setName(String name) {  
  15. this.name = name;  
  16. }  
  17. }  

// index.xhtml

  1. <h:form id="form">  
  2. <h:outputLabel for="username">User Name</h:outputLabel>  
  3. <h:inputText id="name-id" value="#{user.name}">  
  4. <f:validateBean/>  
  5. </h:inputText><br/>  
  6. <h:commandButton value="OK" action="response.xhtml"></h:commandButton>  
  7. </h:form>  





© Javacodegeeks © 2019 - 2019
All Right Reserved and you agree to have read and accepted our term and condition.

Python 3