How to show attribute level validation message programatically in ADF

Scenario: As we know if we have to show attribute level validation message in ADF then we put business rules in Entity Object for the attribute.

Then the message will show like this.


But what if we have to do it programatically. 

It is very easy. Suppose we have to put the validation error for Department Name that if department Name is less than 3 character the the error message should be shown as "Department Name must be greater than three character.

Solution:

Create the EOImpl for the DepartmentEO and go to method:

    public void setDepartmentName(String value) {
        setAttributeInternal(DEPARTMENTNAME, value);
    }

Put your validation logic in this method as follows :

    public void setDepartmentName(String value) {
        setAttributeInternal(DEPARTMENTNAME, value);
        if(value.length()<3){
            AttributeDef[] attrDefs = this.getDefinitionObject().getAttributeDefs();
                                       for (AttributeDef attrDef : attrDefs) {
                                           System.out.println(attrDef.getName());
                                           if (attrDef.getName().equals("DepartmentName")) {
                                               this.registerAttributeException(attrDef, 0,
                                                                               new AttrValException(resourceBundle.getClass(),"ERROR_DEPARTMENTNAME_TOOSMALL",getEntityDef().getFullName(),"DepartmentName",getAttribute("DepartmentName"),null,false));
                                           }
                                       }
        }

    }


In this method we are referring the error message from ResourceBundle.java

you can create a ResourceBundle.java in your project as follows :

package com.kunal.model.bundle;

import java.util.ListResourceBundle;

public class ResourceBundle  extends ListResourceBundle{
    public ResourceBundle() {
        super();
    }

    public Object[][] getContents() {
            return contents;
        }
        static final Object[][] contents = {
            {"ERROR_DEPARTMENTNAME_TOOSMALL", "Department Name must be greater than 3 character"}
        };
}


Now register your ResourceBundle.java in your project properties as shown below.




Now create a jspx page and create a form and provide the CreateInsert and Save button as shown below.



Now when you press CreateInsert button a new row will be created and when you put DepartmentName less than three character and press submit button you will get your message as shown below.




Comments

Popular posts from this blog

Setting up the environment for Angular2 and Hello World Example in Angular2

Showing number of rows or row count on top and bottom of table in ADF.

Build a Simple ReactJS application using react-cli