cancel
Showing results for 
Search instead for 
Did you mean: 

How to set input field to required dynamically?

konchada_saikrishna
Active Participant
0 Kudos

Hi Raj,

How can I make a field as mandatory dynamically.

I saw the field property status = normal/required. Is this is required to change, if so how to change it dynamically.

Thanks & Regards,

Sai.

Message was edited by: Armin Reichert

Accepted Solutions (0)

Answers (4)

Answers (4)

Former Member
0 Kudos

Problem in your code is you are not populating the VisibilityNode properly.

Check the following.

for(int i=0;i<wdContext.nodeFieldsList().size();i++)

{

if(wdContext.nodeFieldsList().getElementAt(i).getAttributeValue("Form_Config.Fld_Check").equals("X"))

wdContext.nodeFieldsVisibility().currentFieldsVisibilityElement().setAccLocV(WDVisibility.VISIBLE);

IPrivate<<ViewName>>.IFieldsVisibilityElement ele= wdContext.createFieldsVisibilityElement();

if(condition)

ele.setAccLocV(WDVisibility.VISIBLE);

else

ele.setAccLocV(WDVisibility.NONE);

wdContext.nodeFieldsVisibility().addElement(ele);

}

Regards, Anilkumar

konchada_saikrishna
Active Participant
0 Kudos

Hi Anil,

Your code worked for me Thank You, but just a small clarification is this works to set the field length also.

Sai Krishna

Konchada Sai Krishna

Former Member
0 Kudos

Hi Sai Krishna,

Are talking about the length property of the InputField?

If so length prop specifies the maximum number of characters to be displayed in the input field.

If this is the one you want then you can do this in the same way.

But if you want to restrict the no.of charecters in a inputfield then you need to create asimpeltype -> set MaxChars and then bind it to inputfield.

Regards, Anilkumar

konchada_saikrishna
Active Participant
0 Kudos

Hi Anil,

There is no proprety like MaxChars listed for the UIElement in the property window.

If such property exists else where please tell me where it is.

Thanks & regards,

Sai krishna

chintan_virani
Active Contributor
0 Kudos

Hi,

You will have to create a simpletype in Local dictionary and set the Maximum length to the length you want.

Next step is to create a contect of the simple type you created and map to the input field.

So ideally the steps are:-

1) Goto dictionaries --> Local Dictionary --> Data Types --> Simple Types

2) Create a new simple type say <i>simtype10 </i>and define the<b> Maximum length</b> as 10 in Length constraints section.

3) Create a context say Ctx_Input of <i>simtype10</i>

4) Map the context to the Input Field.

<b>- Chintan</b>

Former Member
0 Kudos

Hi Sai,

I feel there is a problem in how you are accessing the context in your code.

You are iterating through "FieldsList" node in the for llop and you are trying to access the "FieldsVisibility" node inside the loop .

Please post your cotextStructure so that we can suggest the solution.

Regards, Anilkumar

konchada_saikrishna
Active Participant
0 Kudos

Hi Anil, my Context is like

Context

|

Form_Config(Model node)

FldCheck(Model Attribute of form_config)

FldLen(Model Attribute of form_config)

FldMandatory(Model Attribute of form_config)

|

FieldsVisibility(Value node)

|

_AcclocV(Value attribute of fieldsVisibility node)

_DateV(Value attribute of fieldsVisibility node)

_DescRoleV(Value attribute of fieldsVisibility node)

| ....up to 19 fields

FieldsLength(Value node)

|

_AcclocL(Value attribute of fieldsLength node)

_DateL(Value attribute of fieldsLengthnode)

_DescRoleL(Value attribute of fieldsLengthnode)

| ....up to 19 fields

FieldsMandatory(Value node)

|

_AcclocM(Value attribute of fieldsMandatory node)

_DateM(Value attribute of fieldsMandatorynode)

_DescRoleM(Value attribute of fieldsMandatory node)

| ....up to 19 fields

The node Form_Config gives the various properties like length, visibile, mandatory of the fields and the out put would be 19 rows i.e., 19 fields properties.

Now with this table we need to set the properties of the fields in the view, so I have 3 value nodes named

FieldsVisibility,

FieldsLength, (For maximum length of the field),

FieldsMandatory, each with 19 Value attributes with respect to the 19 rows of Form_Config

So I am iterating in the Form_config Model node, and trying to set the properties of the field.

This is the basic need, If u do have any ambiguity please ask ,

Thanks & Regards,

Sai Krishna.

lajitha_menon
Contributor
0 Kudos

Hi Sai,

Your context and code seem out of place.

You have a form_config node which supplies the field properties like required, visible etc. You have not mentioned what is the key of this node, I suppose there is something like field id or field name, which identifies the line. Now, say that you have a field id. In your code, you are repeatedly setting the field AccLoc's visibility property - which means that this loop will iterate 19 times, and set the same fields property!

From your context and from what you have described in the requirements, you need to make the following changes

1) Check cardinality of nodes. Make sure you have set it to the right one. From your context structure, it looks like all the nodes except form_config can have a cardinality of 1..1 (This way you can avoid the nullpointerexceptions for missing elements)

2) Change your code to match your context to something like this..

//loop in the field_config node

//set lead selection of form_config node

//get value of field id(say its NAME)

if(fieldid.equals("NAME")

{

//get field properties - eg: String Vis = wdContext.currentFormConfigElement().getFldCheck();

if(Vis != null && Vis.equals("X")

wdContext.currentNameVisibility(WdVisibility.VISIBLE);

else

wdContext.currentNameVisibility(WdVisibility.NONE);

..go to next property

}

....do this for all 19 fields.

Regards,

LM

lajitha_menon
Contributor
0 Kudos

Hi, you can do it by putting the following in wdDoModifyView(replace "Customer" with your field id)

IWDInputField Fld = (IWDInputField)view.getElement("Customer");

Fld.setState(WDState.REQUIRED);

However, the required property does not make the field required in the true sense..It just puts a * next to the field..)

Cheers

sridhar_k2
Active Contributor
0 Kudos

Hi,

If you want to keep State Required / Normal on dynamically.

Create a Context Variable, whose type as "com.sap.ide.webdynpro.uielementdefinitions.State" and assign it to your control State property.

Programatically set this like this.

wdContext.currentContextElement().setStateName(WDState.NORMAL);

Regards,

Sridhar

konchada_saikrishna
Active Participant
0 Kudos

Hi L.H,

Even if we set the field status as "REQUIRED", should we handle/validate them manually i.e., programatically. If that is the case, tell me a procedural way to handle this.

i.e., 1) How to check a field for null,

2) After checking how to place cursor at that field,

3) In which method do we need this code.

4) and when I binded a "Inputfield" to a date datatype it popups a date picker, Is there a time picker as well.

Thanks and regards,

Sai krishna. K

sridhar_k2
Active Contributor
0 Kudos

Hi Sai,

Yes, Even if set the status "Required" also.

Lets Say "StateName" is your Context Variable.

Checking null - > if(wdContext.currentContext().getStateName()){

// msgMrg.reportSuccess("Null");

}else{

// msgMrg.reportSuccess("Not Null");

}

2 & 3 Placing the Cursor

Hi,

IWDInputField input = (IWDInputField)view.getElement("SetName");

input.requestFocus();

If you use above method, it just set focus on the input field. You have to use that code in wdDoModifyView().

If you want high light it (Like required Filed) use below code.

setName - Your Context Variable

NameRequired - Error Message

IWDNodeInfo nodeInfo = wdContext.getNodeInfo();

IContextElement contextElement = wdContext.currentContextElement();

String nameVal = contextElement.getsetName();

IWDMessageManager manager = wdComponentAPI.getMessageManager();

if (nodeInfo != null) {

IWDAttributeInfo attributeInfo = nodeInfo.getAttribute("setName");

if (attributeInfo != null) {

ISimpleType simpleType = attributeInfo.getSimpleType();

if (simpleType != null) {

String fieldLabel = simpleType.getFieldLabel();

}

}

if (nameVal == null) {

manager.reportContextAttributeMessage(

contextElement,

attributeInfo,

IMessageTestComp.NameRequired,

new Object[] { "" },

true);

}

}

Regards,

Sridhar

konchada_saikrishna
Active Participant
0 Kudos

Hi sridhar,

When I am trying to set the value attribute to WDVisibility to none, it raises an exception as array out of bound. here is the code I just tried.

for(int i=0;i<wdContext.nodeFieldsList().size();i++)

{

if(wdContext.nodeFieldsList().getElementAt(i).getAttributeValue("Form_Config.Fld_Check").equals("X"))

wdContext.nodeFieldsVisibility().currentFieldsVisibilityElement().setAccLocV(WDVisibility.VISIBLE);

else

wdContext.nodeFieldsVisibility().currentFieldsVisibilityElement().setAccLocV(WDVisibility.NONE);

}

and also I tried as

wdContext.nodeFieldsVisibility().setLeadSelection(0);

wdContext.currentFieldsVisibilityElement().setAccLocV(WDVisibility.NONE);

Please help me,

Thanks & Regards,

Sai Krishna.

sridhar_k2
Active Contributor
0 Kudos

Hi Sai,

I have doubt on your code in two places. Please check them before processing.

//first print the size here.

for(int i=0;i<wdContext.nodeFieldsList().size();i++)

{

if(wdContext.nodeFieldsList().getElementAt(i).getAttributeValue("Form_Config.Fld_Check").equals("X"))

wdContext.nodeFieldsVisibility().currentFieldsVisibilityElement().setAccLocV(WDVisibility.VISIBLE);

// use this code instead of above line. wdContext.currentFieldsVisibilityElement().setAccLocV(WDVisibility.VISIBLE);

//same like in else part.

else

wdContext.nodeFieldsVisibility().currentFieldsVisibilityElement().setAccLocV(WDVisibility.NONE);

}

Regards,

Sridhar

konchada_saikrishna
Active Participant
0 Kudos

Hi Sridhar,

I have tried as you said

wdContext.currentFieldsVisibilityElement().setAccLocV(WDVisibility.VISIBLE);

but gives Null pointer exception as usual.

For a change, I even tried making the ACClocV(value attribute of node fieldsVisibility) as Caliculated. and in the getter method I returned the value as WDVisibility.NONE. the getter method is like

public com.sap.tc.webdynpro.progmodel.api.WDVisibility getFieldsVisibilityAccLocV(IPrivateIncident_Form.IFieldsVisibilityElement element)

{

//@@begin getFieldsVisibilityAccLocV(IPrivateIncident_Form.IFieldsVisibilityElement)

/* if(wdContext.nodeFieldsList().getElementAt(0).equals("X"))

return WDVisibility.VISIBLE;

else */

return WDVisibility.NONE;

//@@end

}

even then the UIElement is visible in the window,

Do we need to call the getter method in the modifyview method especially, if so how is that, i.e., what to send as a parameter, and to whom the return value is to be assigned.

Regards,

Sai krishna.

roberto_tagliento
Active Contributor
0 Kudos

The NULL pointer is because doesn´exist the

wdContext.currentFieldsVisibilityElement()

element!

before do:

wdContext.nodeFieldsVisibility().add(

wdContext.nodeFieldsVisibility().createFieldsVisibilityElement()

);

wdContext.nodeFieldsVisibility().moveLast();

Message was edited by:

Roberto Tagliento

Former Member
0 Kudos

Hi,

Create an attribute map to that status field.

Set it to what ever do u need.

wdContext.currentContext.setFieldName(normal/required).

thanks,

Lohi.