cancel
Showing results for 
Search instead for 
Did you mean: 

Check mandatory dropdownbykey

Former Member
0 Kudos

In my view I have two dropdownbykey elements. After a push on the button I want to check if a value in the dropdown is selected.

I found the following code to check "simple" inputfields:

public void checkMandatory( java.lang.String fieldname )

{

//@@begin checkMandatory()

IWDMessageManager msgMan = wdComponentAPI.getMessageManager();

String value = wdContext.nodeProject().currentProjectElement().getAttributeAsText(fieldname);

IWDAttributeInfo projectAttr = wdContext.nodeProject().getNodeInfo().getAttribute(fieldname);

if (value.length() == 0) {

msgMan.reportContextAttributeMessage(

wdContext.nodeProject().currentProjectElement(),

projectAttr,

IMessageCCAdvice.MISSING_INPUT,

new Object[] { fieldname },

true);

}

//@@end

}

But if I run this a dump appears on

String value = wdContext.nodeProject().currentProjectElement().getAttributeAsText(fieldname);

It works fine for "simple" inputfields but not for the dropdown fields.

Can anybody help me?

Thanks in advance.

Michael

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi

Do dynamic validation this way, its generic for every string attribute, regardless of the UIElement..:


//Method on the view
public void checkMandatory( java.lang.String[] pathNodeNames, java.lang.String attributeName, java.lang.String placeHolder )
  {
    //@@begin checkMandatory()
      //MessageManagerInstance
      IWDMessageManager messageManager =	wdComponentAPI.getMessageManager();
		  
      IWDNode node = wdContext;
      Object attributeValue = null;
      IWDAttributeInfo attributeInfo = null;
		  
      //Find the attribute within context tree
      if (pathNodeNames.length == 0)
      {
      	//Let's get the attribute value		
      	attributeValue = wdContext.currentContextElement().getAttributeValue(attributeName);
      }
      else
      {
	for(int i=0; i< pathNodeNames.length; i++)
	{
	  //Go a level down within context tree 
	  node = node.getChildNode(pathNodeNames<i>, IWDNode.LEAD_SELECTION);
	}
      //Let's get the attribute value		
      attributeValue = node.getCurrentElement().getAttributeValue(attributeName);
      }
      //Extract attribute info
      attributeInfo = node.getNodeInfo().getAttribute(attributeName);
			
      //Check if the string is null or empty, and report nulls/empties to messagemanager
      if (isEmptyField(attributeValue))	
      {
      	messageManager.reportContextAttributeMessage(node.getCurrentElement(), 
			attributeInfo, IMessageCCAdvice.MISSING_INPUT, new Object[] { placeHolder }, true);
      }					  
    //@@end
  }

  
  //Metodo on the view
public boolean isEmptyField( java.lang.Object attributeValue )
  {
    //@@begin isEmptyField()
      String stringValue;
      if ((attributeValue != null))
      {	
      	stringValue = attributeValue.toString();
      	if (stringValue.trim().length() > 0)
      	{
      	  return false;
      	}
      }
      return true;	
    //@@end
  }
  
  //Method which will manage dynamic validation to achieve business rules
   public void doValidateFields( )
  {
      if (someCondition())  //You can validate based on conditions, in order to apply business rules sucessfully
      {	
        //Array of Strings would go empty if the attribute is located in context root
        //wdContext -->attribute
        this.checkMandatory(new String[]{} , fieldname , "placeholderValue");
      }
      else
      {
        //Careful here, you'll send one element inside the array of String for each subnode in context tree in which attributeName is located
        //on your case, within context root you have a node called "Project", and your attributeName inside "Project"
        //wdContext-->node "Project"--> attribute "fieldName"
        checkMandatory(new String[]{"Project"}, "fieldName", "placeholder text of your choice if needed");

        //Show error messages if thats the case
      	wdComponentAPI.getMessageManager().raisePendingException();
      }
  }

Just call the method doValidateFields whenever you want to apply dynamic validation.


  public void onActionRejectLoanRequest(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent )
  {
    //@@begin onActionRejectLoanRequest(ServerEvent)
	//Execute validations
	doValidateFields();
		
	//ATTENTION:  If at least one field didn't pass validation,
	//                    this Action's code will stop HERE, next lines
	//                    won't execute. Nice uh?
		
	//Call method of Loan Reject in CompController to execute reject actions
	wdThis.wdGetLoansAdministrationCompController().doRejectRequest();
    //@@end
  }

Normally, i use some generic error message, and pass the name or description of the value i'm validating, something like:

"The value is mandatory, please select or enter a valid value".

It could be generic too (passed as parameter) , with a few more refinement of this method.

Regards

Julio C. Herrera Cuevas

Former Member
0 Kudos

Thanks Julio.

It works. Great!

Answers (2)

Answers (2)

former_member197348
Active Contributor
0 Kudos

Do null checks like this:

public void checkMandatory( java.lang.String fieldname )

{

//@@begin checkMandatory()

IWDMessageManager msgMan = wdComponentAPI.getMessageManager();

String value = "" ;

if(wdContext.nodeProject().currentProjectElement()!=null) {

value = wdContext.nodeProject().currentProjectElement().getAttributeAsText(fieldname);

}

IWDAttributeInfo projectAttr = null;

if(wdContext.nodeProject().getNodeInfo().getAttribute(fieldname)!=null)

{

projectAttr = wdContext.nodeProject().getNodeInfo().getAttribute(fieldname);

}

if (value.length() == 0) {

msgMan.reportContextAttributeMessage(

wdContext.nodeProject().currentProjectElement(),

projectAttr,

IMessageCCAdvice.MISSING_INPUT,

new Object[] { fieldname },

true);

}

//@@end

}

regards,

Siva

Former Member
0 Kudos

Unfortunally, this doesn't work, it still stucks on getAttributeAsText

edit:

I don't know if it matter but the context attribute is associated to a simple type manually created to fill the values of type string.

Edited by: M. den Ouden on Jan 9, 2008 4:52 PM

Former Member
0 Kudos

Hi,

Please use following code

if(wdContext.nodeProject()!=null)

if(wdContext.currentProjectElement()..getFieldName()!=null)

{

check here for "-1" or select, you have put one

}