cancel
Showing results for 
Search instead for 
Did you mean: 

How to make a field Mandatory dynamically

Former Member
0 Kudos

Hi Guys,

              I have a requirement to make a field mandatory depends on the value of some other field.

is it possible to make it mandatory dynamically.

Thanks

Sam

Accepted Solutions (0)

Answers (3)

Answers (3)

amy_king
Active Contributor
0 Kudos

Hi Sam,

I have done this in a component using this approach:

  1. Define a context attribute, STATE, of type WDUI_STATE and bind the attribute to your field's state property. The state property controls whether the UI element is required or normal.
  2. Write a method, say check_required, that reads the dependent field value and sets the value of context attribute STATE to either '00' (normal) or '01' (required).
  3. You'll need either an onEnter event or some other event which calls method check_required when a value is entered into the dependent field. In my application, I have a tabstrip and I call my check_required method whenever the user navigates from tab to tab so that I'm guaranteed to have called check_required by the time the user reaches the desired tab.

If you want to prevent the user from navigating away from a view with a required field, you need to issue an error message, say in WDDOBEFOREACTION, for an action that is defined with action type = standard (as opposed to validation-independent). A standard action will prevent the user from leaving a view with an error message.

Cheers,

Amy

former_member185241
Active Participant
0 Kudos

Hi Sam Vel

In the View controller create on atrribute STATE with type STRING. For that Input field for which u need mandatory in the Properties For the State bind the Attribute STATE.

And based on condition make field as dynamic

DATA lo_el_context TYPE REF TO if_wd_context_element.

* get element via lead selection

  lo_el_context = wd_context->get_element(  ).

if  <Condition> IS TRUE.

* set single attribute

  lo_el_context->set_attribute(

      name =  `STATE`

      value = 'required' ).

else.

* set single attribute

  lo_el_context->set_attribute(

      name =  `STATE`

      value = 'Normal Item' ).

endif.

OR Else

you can set the index of drop down( State property is Drop down only no) So depending on your situation you can set the drop down value

01 for Normal

02 for Required Item

Thanks

Abhishek

Former Member
0 Kudos

Hello Sam,

                 you can try this, Create attribute in the context node and bind the state property of the input field with attribute and depending on the value of some other field put value as required in the created attribute

But you need to set this value depending on some event and in the event handler method you can write the code of setting the value

Former Member
0 Kudos

That's a good idea. wondering what's the data type for Required Filed..?

WDY_BOOLEAN..?

Former Member
0 Kudos

Hello sam

  try wdy_boolean it may work because if you see there are only two values for that normal item and required.

Former Member
0 Kudos

we have to use WDUI_STATE. But I am using FPM so even though I set it dynamically, still the user can navigate between views using FPM controls.

Not sure how to control this..!

Former Member
0 Kudos

Hi Sam,

1. After binding the state property of the input field with attribute of type WDUI_STATE, You have to use code for checking is mandatory fields are initial or not for that you can use "check_mandatory_attr_on_view" method of  class cl_wd_dynamic_tool.

You can write it in PROCESS_EVENT method of  component controller.
It will also stop the navigation problem.

2. If you want to stop navigation of FPM main step that you can control separately, Using Enable Check box of main step at FPM configuration page (Do it for all main steps).

Ref thread : http://scn.sap.com/thread/3216442    

Former Member
0 Kudos

Hi Mate,

              I done both already.. I think its probably because of Dynamically making it mandatory. For the other Mandatory fields its working..!

Thanks

Sam

Former Member
0 Kudos

Hi,

Plz check the state property of attribute is it binded properly or not, Check it in Line no 22 and 23 of the earlier mentioned method using debugger. ( Condition on 23 line should be true )

That might be a reason. plz revert back if i am wrong.

Regards,

Nishant

Former Member
0 Kudos

Hi Mate,

               I check that the values are updated in my context to '01' .

But somehow its not checking it in the Process_Event method.

Thanks

Sam

Former Member
0 Kudos

Hi Sam,

Did you try using the method 'CHECK_MANDATORY_ATTRIBUTES' of CL_WD_DYNAMIC_TOOL to check the field which you need to make mandatory dynamically? In this method you can exclusively provide the attribute list to be checked.

Regards,

Priyanka

Former Member
0 Kudos

Hi,

System will check this method only if any FPM event is been raised, if u want explicitly call this method use following code

data lo_fpm type ref to if_fpm.

  lo_fpm = cl_fpm_factory=>get_instance( ).

  lo_fpm->raise_event_by_id( if_fpm_constants=>gc_event-leave_initial_screen ).

Here the bold part is the event id which you can change accordingly.