Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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. 

Just as any other data control customization this requires an annotated method in a Customization Java Class

Code Block
languagejava
    @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:
Image Added