donderdag 20 maart 2014

ADF - Throwing a validation exception from a view row object

In ADF, you can easily add method validators on Entity level. However, sometimes this is not enough. If your validation logic is complex (for instance: if you need all other rows to validate against, for some kind of special uniqueness constraint), this is not enough.

You can throw a general exception from the setter of your attribute in the view object, but that is not enough, because this will look different (no 'red' field, and a different style of exception on the screen).

To solve this, you will need to throw an 'AttrValException', as shown below:

General method in the view row base class.

Final static String RESOURCE_BUNDLE_NAME = "my_resource_bundle";
protected void throwValidationException(String message, String attrName) {
    PropertiesBundleDef resBundle = new PropertiesBundleDef(this.getViewDef());
    throw new AttrValException(MetaObjectBase.TYP_VIEW_OBJECT, resBundle, message, getViewObject().getFullName(), attrName);

Setter of view row object:

public void setMyCode(String value){
    if(/* your validation logic */){
       throwValidationException("myCodeValidationErrorMsgKey", "MyCode");
    setAttributeInternal(MY_CODE, value);