Designer 10.5 | webMethods CAF and OpenUI Development | Working with Facelets | Java Annotations in CAF and webMethods OpenUI Applications | Bean Validation Standard Annotations
 
Bean Validation Standard Annotations
The JSR-349 Bean Validation standard defines a set of annotations that can be used to declare and configure validation constraints for fields in beans. This is an alternative to declaring the validators for each input control in the UI templates of your Composite Application Framework (CAF) application. Instead of defining the validators within the .view or .xhtml file, you instead define them on the field in the backing bean that the input controls are bound to. Pertinent code is highlighted in bold.
Example 1:
package caf.war.testapp1.test1;
 
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import javax.validation.constraints.Pattern;
 
import com.webmethods.caf.faces.annotations.ExpireWithPageFlow;
import com.webmethods.caf.faces.bean.BaseFacesBean;
 
@ManagedBean(name="inputBeanValidatorBean")
@SessionScoped
@ExpireWithPageFlow
public class InputBeanValidator extends BaseFacesBean {
    @Pattern(regexp = "\\d{3}-\\d{3}-\\d{4}")
    private String value = null;
 
    public String getValue() {
    return value;
    }
 
    public void setValue(String value) {
    this.value = value;
    }
}
Example 2:
@ManagedBean(name = "user")
@SessionScoped
public class UserBean implements Serializable {
    private static final long serialVersionUID = 1L;
 
    //Check the size of a string.
    @Size(min=3, message="Enter minimum 3 characters!")
    private String name;
 
    //Check that a value is at least or at most the given bound.
    @Min(10)
    @Max(1000)
    private double amount;
 
    //Check that a date is in the future.
    @Future
    private Date date;
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public double getAmount() {
        return amount;
    }
 
    public void setAmount(double amount) {
        this.amount = amount;
    }
 
    public Date getDate() {
        return date;
    }
 
    public void setDate(Date date) {
        this.date = date;
    }
}
Related Topics