Skip to content

Validation

Michael Karneim edited this page May 9, 2018 · 5 revisions

Beanfabrics provides an easy-to-use framework for validating input data.

Any PM component can be configured with a set of validation rules. These rules are evaluated whenever the PM or one of its siblings has been changed, and produce a validation state. This state is stored in the PM and can be queried by application code. For example most of the GUI components show the validation state of its bound PM component with a small red overlay icon.

---!!!--- TODO include ValidationOverlayIcon.png ---!!!---

How to define a validation rule?

Validation rules can be defined

  1. by Using the @Validation annotation
  2. or by Using the Validator API

Using the @Validation annotation

In the following example we define a PM that accepts a positive integer number.

package wiki.validation.anno;

import org.beanfabrics.model.IntegerPM;
import org.beanfabrics.model.PMManager;
import org.beanfabrics.support.Validation;

public class PositiveIntegerPM extends IntegerPM {
    
    public PositiveIntegerPM() {
        PMManager.setup(this);
    }

    @Validation
    public boolean isPositive() {
        return this.getBigInteger().signum() > 0;
    }

}

By extending IntegerPM this class inherits the standard validation rules for integer numbers. These rules already validate that the value is numerical and has no places after the decimal point. To narrow these constraints to positive numbers, this class defines a new validation method isPositive(). By annotating this method with @Validation it will be appended to the end of the existing rules inherited from IntegerPM during the process of PMManager.setup(this).

The following code illustrates the behaviour of PositiveIntegerPM.

PositiveIntegerPM model = new PositiveIntegerPM();                                      
model.setText("1234");                                                                  
System.out.println(model.getText()+(model.isValid()?" is ":" is not ")+"valid");        
                                                                                        
model.setText("-1234");                                                                 
System.out.println(model.getText()+(model.isValid()?" is ":" is not ")+"valid");        

This is the output:

1234 is valid
-1234 is not valid

Using the Validator API

The same result can be achived by calling the Validator API directly.

package wiki.validation.api;

import org.beanfabrics.model.IntegerPM;
import org.beanfabrics.model.PMManager;
import org.beanfabrics.validation.ValidationRule;
import org.beanfabrics.validation.ValidationState;

public class PositiveIntegerPM extends IntegerPM {
    
    public PositiveIntegerPM() {
        PMManager.setup(this);
        
        this.getValidator().add( new ValidationRule() {        
            public ValidationState validate() {
                if ( !isPositive()) {
                    return new ValidationState("This value is not positive"); 
                }
                return null;
            }
        });
    }
    
    public boolean isPositive() {
        return this.getBigInteger().signum() > 0;
    }

}

The difference to the previous example is that the validation rule is manually defined and added to the Validator. It has to conform with the ValidationRule interface and return a ValidationState object to indicate that the validation failed, or null if it passed.