TO ADD CUSTOM VALIDATORS IN ADF
The Code is
tested on ADF 11.1.2.4.0 version.
Sometimes Business
rule cannot be applied on Entity object to throw out error as per customer
requirements.
We need to
add custom validations to throw error during run time for an application.
Below code
represents a scenario where I want to throw an error at run time if the Item quantity
is in decimal point for particular Item UOM(Unit of measure) .
public void ValidatorReqQty(FacesContext
facesContext,
UIComponent uIComponent,
Object object) throws ValidatorException
{
DCBindingContainer bindings4 =
(DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
DCIteratorBinding dcItteratorBindings =
bindings4.findIteratorBinding("XXPRIVI_MATERIAL_REQUESTView1Iterator");
ViewObject XXPRIVI_MATERIAL_REQUESTView1 =
dcItteratorBindings.getViewObject();
Row dv =
XXPRIVI_MATERIAL_REQUESTView1.getCurrentRow();
FacesContext fc = FacesContext.getCurrentInstance();
BigDecimal ReqQuantityCurr =
(BigDecimal)object;
System.out.println("ReqQuantityCurr" + ReqQuantityCurr);
String ReqQuantityCurrStr =
(String)ReqQuantityCurr.toString();
System.out.println("ReqQuantityCurrStr"
+ ReqQuantityCurrStr);
String ReqQuantityCurrStrWithDec =
(String)ReqQuantityCurr.setScale(2,BigDecimal.ROUND_UP).toString();
System.out.println("ReqQuantityCurrStrWithDec"
+ ReqQuantityCurrStrWithDec);
String DecnumberCurr = ReqQuantityCurrStrWithDec.substring(ReqQuantityCurrStrWithDec.indexOf(".")).substring(1);
System.out.println("DecnumberCurr"
+ DecnumberCurr );
String CompareUom = "EA" ;
if(Uom.equals(CompareUom) && DecnumberCurr.compareTo("00")
!= 0)
{
throw
new ValidatorException(new FacesMessage("Quantity cannot be in
decimal for UOM 'EA'."));
}
}
Now Run the
project.
The below image represents the validation error as per the
requirement.
Love ADF... :)
Nice Article for beginners. Congratulations to S.S.C. !!! :)
ReplyDeleteThanks a lot..:)
ReplyDelete