on 06-10-2009 11:54 AM
Hi,
I am new to WebDynpro and am trying to do validation for my 4 fields,that they should not be left empty.
Here as a first step,i have created a key in the message pool with text"Field cannot be empty."
2.Also created a method in the view called checkMandatory of the type boolean and parameter fieldName.
In the view i implemented the following:
public boolean checkMandatory( java.lang.String fieldName )
{IWDMessageManager msgMan = wdComponentAPI.getMessageManager();
String fieldname = "FirstName";
String FirstName = wdContext.nodeEmployee().currentEmployeeElement().getAttributeAsText(fieldname);
IWDAttributeInfo employeeAttr = wdContext.nodeEmployee().getNodeInfo().getAttribute(fieldname);
if(FirstName.length()==0)
{msgMan.reportContextAttributeMessage(wdContext.nodeEmployee().currentEmployeeElement().getAttributePointer(fieldname), IMessageEmployee.MISSING_INPUT, new Object[]{fieldname});
return false;
}
return true;
3.I checked the method in the save method in the view
public void onActionSave(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent )
{
//@@begin onActionSave(ServerEvent)
if(checkMandatory(null))
{ IEmployeeElement E1 = wdContext.nodeEmployee().createAndAddEmployeeElement();
wdContext.nodeEmployee().setLeadSelection(E1.index());
}
Is my approach correct?
How should i go ahead with checking the same for other fields?
Request Help.
Regards,
Suvarna
Hi Suvarna,
The data is getting saved in the backend because of the code that you provided in the save action
if(checkMandatory(null))
{ IEmployeeElement E1 = wdContext.nodeEmployee().createAndAddEmployeeElement();
wdContext.nodeEmployee().setLeadSelection(E1.index());
}
this if condition will return true because your first input field is not empty hence goes and saves the data
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi
I told you to try this:
public void onActionSave(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent ){
if(checkMandatory("FirstName") && checkMandatory("LastName")
&&checkMandatory("ID") && checkMandatory("Gender")){
IEmployeeElement E1 =
wdContext.nodeEmployee().createAndAddEmployeeElement();
wdContext.nodeEmployee().setLeadSelection(E1.index());
}
Now, the operator && exhibit "short-circuiting" behavior, which means that the second operand is evaluated only if needed.
Though if checkMandatory in one case returns false the "if" statement would evalute as "false" without executing the other checkMandatory to the right,
To evalute all the fields this would be the solution:
public void onActionSave(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent ){
boolean fnb = checkMandatory("FirstName");
boolean lnb = checkMandatory("LastName");
boolean idb = checkMandatory("ID");
boolean geb = checkMandatory("Gender");
if(fnb && lnb && idb && geb){
IEmployeeElement E1 =
wdContext.nodeEmployee().createAndAddEmployeeElement();
wdContext.nodeEmployee().setLeadSelection(E1.index());
}
Now, if the "if" statement evalutes as false and you are saving to the table inside the if then I have no explanation to it. Post some code if you want
Try with this:
public void onActionSave(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent ){
if(checkMandatory("Parameter1") && checkMandatory("Parameter2")
&&checkMandatory("Parameter3") && checkMandatory("Parameter4")){
IEmployeeElement E1 =
wdContext.nodeEmployee().createAndAddEmployeeElement();
wdContext.nodeEmployee().setLeadSelection(E1.index());
}
Dont override the checkMandatory variable
String fieldname = "FirstName";
This method should help you make the check generic
NOTE: All the fields that you are checking on the checkMandatory method are refering the note Employee, dont try to use the method with parameters of another node.
Edited by: Jean Carlo Abreu on Jun 10, 2009 1:07 PM
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks a lot for the help!!!
Now validation is working for all my fields.
But i have another problem now.I have 4 fields namely FirstName ,LastName,ID and Gender.
Here if i give value for FirstName and try to save.It gives me error saying that LastName is empty but it is getting saved in the table.
Can you please help.
Thanks,
Suvarna
Hi Suvarna,
Try this.
public boolean validateFields( )
{
//@@begin validateFields()
String fname = wdContext.currentPersonElement().getFname();
String lname = wdContext.currentPersonElement().getLname();
String no = wdContext.currentPersonElement().getID();
String gender = wdContext.currentPersonElement().getGender();
boolean flag = true;
if(fname == null || fname.equals("") || lname == null || lname.equals("") || gender == null || gender.equals("") ||
no == null ||no.equals(""))
{
wdComponentAPI.getMessageManager().reportMessage(IMessageTestComp.MISSING__INPUT,null,true);
flag = false;
}
else
flag = true;
return flag;
//@@end
}
Inside Message Pool
Key : MISSING__INPUT
Message : None of the fields can be blank.
Inside Action
public void onActionSave(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent )
{
if(validateFields())
{
// code for Proceed save
}
//@@end
}
Regards,
Mithu
hi Suvarna
You can make the following changes to make ur method generic
public boolean checkMandatory( java.lang.String fieldName )
if (fieldName != null)
String value = wdContext.nodePerson().currentPersonElement().getAttributeAsText(fieldName);
if(value == null || value.length() == 0)
raise error message
public void onActionSave(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent )
this.CheckMandatory("1st fieled name");
this.CheckMandatory("2nd fieled name");
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
85 | |
10 | |
10 | |
9 | |
6 | |
6 | |
6 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.