Friday, 15 January 2016

Validators

  • Case1: Validate from java code
    • Create java class extend from javax.faces.validator
    • Add the logic(implement) validate() method
    • Open faces-config.xml -> validators tab ->  provide ID's to the class names
    • Navigate to any attribute in .jsff page -> insert validator -> provide the validator id OR drag and drop email validator and provide regular expression 
  • Case2: Validate from DB
    • Create a jsp page with 2 input text fields, employeeId, employeeName
    • Create a employeeVO on Employees table with a employeeID bind and view criteria with condition on bind variable
    • Create a java class for the vo -  employeeViewImpl.java 
    • Create application module impl class, and create 2 methods - validateEmployeeID & validateEmployeeName
public boolean validateEmployeeId(int empId){
     EmployeeViewImpl emp = getEmployeeView1();
     emp.setApplyViewCriteriaName("findEmpByIdCriteria");
     emp.setNamedWhereClauseParam("b_employeeId",empId);
    emp.executeQuery();
      if (emp.getRowCount()>0){
          return true;
       } else { return false;
}
}
    • Expose the methods to client interface in AM
    • Crete Validator java classes extending from javax.faces.validator
    • Add following method to Validator class
public boolean getMethodResult(int empId){
  OperationBinding oper = ADFUtils.findOperation("validateEmployeeId); //binding
  oper.getParamMap().put("empId",empId);
  Object result = oper.execute();
  return Boolean.valueOf(oper.getResult().toString());
}
}

    • call getMethodResult from validate() -> throw ValidatorException if method returns false
    • Add the validator class files as Managed beans in adfc-config.xml
    • Navigate to jsff, input text field, set validator property of the filed to managed bean validate method.

No comments:

Post a Comment