20 Feb 2011

ADF BC. Adding validation rules to Entity object.

If you need to add validation rule to programmatically created Entity object or you just have to add some rules to the entity's attribute "on the fly",  you should use implementing classes of JboValidatorInterface. ADF Model's Java API has appropriate implementing class for each validation type. If you need something "special", you can create your own implementation of JboValidationInterface.
In this post I'm going to show a couple of examples of using JboValidator classes.

The following method creates validator to control the maximum length of some entity's attribute:
private JboLengthValidator getLengthValidator(Integer maxLength) {
        //Create validator    
        JboLengthValidator jlv = new JboLengthValidator();
        //Set operation type
        //Set the value to compare with
        //Refer to error message in resource bundle 
        //Populate parameter value for error message
        HashMap errvaluesMap = new HashMap();
        errvaluesMap.put("0", maxLength.toString());         
        return jlv;

    //The key in your resource bundle properties file.
    // It contains error message like this:
    // maxlength_err=The maximum length is {0} characters 
    private static final String MAX_LENGTH_ERROR="maxlength_err"; 

The next validator is responsible for checking minimum value of some attribute:
private JboCompareValidator getMinValueValidator(Integer minValue) {
        JboCompareValidator jcv = 
            new JboCompareValidator(false,
        HashMap errvaluesMap = new HashMap();
        errvaluesMap.put("0", minValue.toString());         
        return jcv;
    //minvalue_err=The minimum value is {0}
    private static final String MIN_VALUE_ERROR="minvalue_err";    

And at last the piece of code to add the validators to some attribute:
private void addValidators(AttributeDefImpl at, 
                               Integer maxLength, 
                               Integer minValue) {
        if (maxLength!=0) 

        if (minValue!=0) 

That's it!

No comments:

Post a Comment

Post Comment