cancel
Showing results for 
Search instead for 
Did you mean: 

Validating required fields

Former Member
0 Kudos

Hi,

in my web dynpro application are lots of required fields.

How could I check if they were filled?

I hope there is a loop-solution, or someting else,

which guide me to every UI-Element to check.

Thanks Steve

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hello Steve

Try this :

Have a method say 'reportException' with input parameter of type 'String' where you report exception of null attributes.

Then, where-ever you want to do validate fields , there you run a loop of the size of node containing the attributes bound to the fields that require validation.

for(int i=0; i< nodeSize; i ++)

{

<get the element at i from the node, say nodeElement)>

if(null == nodeElement.attribute || nodeElement.attribute.length <= 0)

{

this.reportException(nodeElement.attibute);

}

Please note, in the reportException, use message manager's reportInvalidExceptionAttribute() in order to report all null fields at a time.

I hope this helps you.

Regards

Kapil

sridhar_k2
Active Contributor
0 Kudos

Hi Kapil,

How it works??? I didn't understand the logic getting context elements in the for loop.

Steve,

Try the following code.Here you don't need to write method for every field. You just need to call this method for all fields.

fieldName - is the Context Variable

fieldValue - is the Value

columnName - For which Column you want to show error msg. Ex: - "Name is required". Name - is Column Name

call this method like this.

wdThis.checkMandatory(contextElement.NAME,name,"Name");

// For all your entries, call this method.


public void checkMandatory( java.lang.String fieldName, java.lang.String fieldValue, java.lang.String columnName ){
//@@begin checkMandatory()
  IWDMessageManager manager = wdComponentAPI.getMessageManager();
  IWDAttributeInfo attributeInfo = null;
  IWDNodeInfo iNodeInfo = wdContext.getNodeInfo();
  if (fieldValue == null){ 
     fieldValue = "";
   }
   if (fieldName == null){
     fieldName = "";
	 }
   if (contextElement != null){
      if (iNodeInfo != null){
          attributeInfo = iNodeInfo.getAttribute (fieldName);        
      }
     if (fieldValue.length() == 0 && attributeInfo != null){
     manager.reportContextAttributeMessage(
     contextElement,
     attributeInfo,
     IMessageCompany.MISSING__INPUT,
     new Object[] { columnName },
     true);
    }  
}
//@@end
} 

Note : In the Message Pool declare a Msg as 'is required.' Msg Type - Error.

Regards,

Sridhar

Message was edited by: Sridhar kanchanapalli

Former Member
0 Kudos

Hi Sridhar,

but in your solution it is also necessary to know the fieldnames. I search for a solution, where the fieldnames (UI fieldnames)could be unknown.

I that case the code is very flexible against changes in UI-Design.

Thanks

Steve

Former Member
0 Kudos

Please let me know if I have understood your question correctly.

a) All that you want to check is whether the user has filled the input field ? Only if the user has not filled the input field , throw an error. Is that right ?

If that is true, wouldn't the <b>state</b> of input field help ? Set the state to required.

Regards,

Subramanian V.

sridhar_k2
Active Contributor
0 Kudos

Hi Steve,

I dont think, we have any readymade solution for your requirement.

In my earlier post, you have to call that method(checkMandatory(...) at least once for the field.

Regards,

Sridhar

Former Member
0 Kudos

Hi Subramanian,

thats exactly what I have done, but how can I validate that fields. When I press an action button, I got no message or something else --> How can I validate the field state (can I throw an event?), it doesnt work automatically?

Bye Steve

saraswathi_d
Participant
0 Kudos

Hi,

You have to use portal Eventing

Receiver side : wdPortalEventing.fire()

Sender Side: wdPortalEventing.subscribe()

You have to set the action for button. Write the code in that action. This is the sample code.

public void onActionShow(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent

wdEvent )

{

//@@begin onActionShow(ServerEvent)

String name = wdContext.currentContextElement().getName();

WDPortalEventing.fire("urn:com.sap.tc.webdynpro.example.portaleventing",

"Show",

name);

//@@end

}

public void wdDoInit()

{

//@@begin wdDoInit()

WDPortalEventing.subscribe("urn:com.sap.tc.webdynpro.example.portaleventing",

"Show",

wdThis.wdGetReactPortalEventingAction() );

//@@end

}

public void

onActionReactPortalEventing(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent

wdEvent, java.lang.String dataObject )

{

//@@begin onActionReactPortalEventing(ServerEvent)

wdContext.currentContextElement().setName(dataObject);

//@@end

}

Regards,

Saraswathi.

Former Member
0 Kudos

Hi Steve,

How do you mark your fields as mandatory?

Do you set "state" attribute of InputField to "required"? Then you could run through all view elements in wdDoModifiyView the first time, and for each InputField encountered check the "state" property. If "state" is required, add the bindingOfValue to some list. Store that list in your context.

Now if you want to check required fields in an action handler you loop through the list, for each element find the matching context attribute and check its value.

You can then use MessageManager to point out missing items on the screen.

This is the most generic solution I can think about.

Might be best to think about a(n already existing?) model that supports these checks, though. Bind your UI elements via context binding, context mapping, model binding to your model and let the model do the work.

Keeping model and UI in sync in terms of user feedback (marking required fields with "*", pointing out missing items on the screen) still will be a challenge.

Regards

Markus

Answers (2)

Answers (2)

Former Member
0 Kudos

Steve,

Look at <a href="http://help.sap.com/saphelp_erp2005vp/helpdata/en/df/5893426f257073e10000000a155106/content.htm">Non-Validating and Validating Actions</a> and <a href="http://help.sap.com/saphelp_erp2005vp/helpdata/en/0d/5375848e41aa4c86219c80acd054df/content.htm">Validation Example</a>

Bala

Former Member
0 Kudos

Hi Bala,

thanks for your ideas, but I'm not searching for that.

There must be a comfortable solution for implementing such an use case, otherwise you cannot implement flexible applications with web dynpro.

For me its not plausible to name all contextelemets within a method, to get the required option.

In hope for a good solution

Steve

Former Member
0 Kudos

Hi

You have to check it in your code.

if(wdContext.currentContextElement().getParam()==null){

wdComponentAPI.getMessageManager().reportSuccess("Please Enter the Field");

}else{

//Do the assign

}

See this Thread

Kind Regards

Mukesh

Former Member
0 Kudos

Hi Mukesh,

I know that solution.

But when you have got 100 Fields and 50 of them

mandatory, ... then its very uncomfortable.

I search for an loop-based soltion, within I go from ui-element to ui-element and check the state-content.

Thanks Steve