Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Next »

Now lets work on an attribute validation. Lets create a rule that the salary of an employee can not be lowered (we would all like that, wouldn't we?). 

An attribute validation is a validation specific for one attribute, if you want to have validation over multiple attributes or an element, you need to use the ElementValidation customization.
You will not see this attribute validation in the DataControl, but once the attribute on the page is submitted, the validation will fire.
If the validation fails, it is up to you as programmer to throw the correct error message as an AttrValException. 

     @AttrValidation
    public void validateSalary(AttrChangeEvent event) throws AttrValException {
        BigDecimal oldVal = (BigDecimal) event.getOldValue();
        BigDecimal newVal = (BigDecimal) event.getNewValue();
        if (newVal != null && oldVal != null && newVal.compareTo(oldVal) < 0) {
            throw new AttrValException(AttrValException.TYP_ATTRIBUTE, "Salary can not be lowered", "SAL-0001",
                                       event.getElement().getDefinition().getFullName(), event.getAttribute());
        }
    }


The signature of an AttrValidation is very specific, the name of the method needs to have the prefix 'validate' followed by the attribute name.
The parameter is an AttrChangeEvent which holds the old value and the new value as well as the Attribute and the Element. The AttrChangeEvent is imported from the package: 'org.adfemg.datacontrol.xml.events.AttrChangeEvent'.
Your method also needs to throw an AttrValException or an AttrSetValException which is an subtype of the AttrValException. Most of their constructors require a resource bundle (class), but their is one simple alternative without using a resource bundle as shown in the code example above. 
The method needs to have the annotation '@AttrValidation', after adding the annotation you should see an import appear from the class: 'org.adfemg.datacontrol.xml.annotation.AttrValidation'. 

Rebuild your Java class.
Now run the page.
You will notice that if you want to lower the salary and then press the submit button, you will get an error on the salary field:
 


  • No labels

0 Comments

You are not logged in. Any changes you make will be marked as anonymous. You may want to Log In if you already have an account.