Class ConstrainedProperty

java.lang.Object
rife.validation.ConstrainedProperty
All Implemented Interfaces:
Cloneable

public class ConstrainedProperty extends Object implements Cloneable
A ConstrainedProperty object makes it possible to easily define all constraints for a named property of a bean.

The property name refers to the actual name of the bean property. However, this sometimes doesn't correspond to its conceptual usage. It can be handy to receive constraint violation reports with another conceptual name: the subject name. Notice that this corresponds to the subject that is used in a ValidationError. If no subject name is specified, the property name will be used instead.

It's possible to add constraints to a ConstrainedProperty instance through regular setters, but chainable setters are also available to make it possible to easily define a series of constraints, for example:

ConstrainedProperty constrained = new ConstrainedProperty("password")
    .maxLength(8)
    .notNull(true);

Constrained properties are typically added to a Constrained bean in its constructor. These are the static constraints that will be set for each and every instance of the bean. You'll however most of the time use the MetaData class that provides the activateMetaData method which initializes the constraints on a need-to-have basis. This dramatically reduces memory usage since otherwise all constraints will be initialized for every bean instance, even though you don't use them, for example:

public class Credentials extends MetaData
 {
    private String login_ = null;
    private String password_ = null;
    private String language_ = null;

    public Credentials() {
    }

    public activateMetaData() {
        addConstraint(new ConstrainedProperty("login").maxLength(6).notNull(true));
        addConstraint(new ConstrainedProperty("password").maxLength(8).notNull(true));
        addConstraint(new ConstrainedProperty("language").notNull(true));
    }

    public void setLogin(String login) { login_ = login; }
    public String getLogin() { return login_; }
    public void setPassword(String password) { password_ = password; }
    public String getPassword() { return password_; }
    public void setLanguage(String language) { language_ = language; }
    public String getLanguage() { return language_; }
 }

It's however also possible to add constraints to a single bean instance whenever they can't be determined beforehand. These are then dynamic constraints than can be populated at runtime, for example:

Credentials credentials = new Credentials();
 credentials.addConstraint(new ConstrainedProperty("language").inList(new String[] {"nl", "fr", "en"}));
 
Since:
1.0
See Also: