Skip to Content
author's profile photo Former Member
Former Member

Check mandatory dropdownbykey

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

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

3 Answers

  • Best Answer
    Posted on Jan 10, 2008 at 12:03 AM

    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

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 09, 2008 at 02:22 PM

    Hi,

    Please use following code

    if(wdContext.nodeProject()!=null)

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

    {

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

    }

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 09, 2008 at 02:23 PM

    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

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member

      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

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.