setMandatory is not working in my onchange script

John Gomolon
Giga Contributor

This my onChange Client script that makes the comment field mandatory if correct state is selected.

My problem is that if i select state(1,2,4)   the comment field will become mandatory and I select state(3), it wont become mandatory.

Could you help me on this, i don't know the root cause of this script because they are in the same IF.

Here is my code:

function onChange(control, oldValue, newValue, isLoading) {

  if (isLoading || newValue == '') {

  return;

  }

var state = newValue;

if(state == '1' || state == '2' || state == '3' || state == '4'){

    form.setMandatory('comments', true);

  }

}

thanks,

12 REPLIES 12

ohhgr
Kilo Sage
Kilo Sage

Hi John,



I'm sure it is just a typo, but if not, please use g_form.setMandatory("comments", true);



Also, since this script is working for other states, check if there's any UI policy or other script that might be causing comments to not be mandatory.



Hope that helps.


Mandar


Sorry for the typo



There is no UI policy running. just the Script.


Hi John,



Let us simplify this even further. Put two alerts before and after the if condition to check if the control reaches till the comments part.



var stateValue = newValue;


alert("State is "+stateValue);


if(stateValue == '1' || stateValue == '2' || stateValue == '3' || stateValue == '4'){


alert("should set comments mandatory");


    form.setMandatory('comments', true);


}



See, what responses you get for states 1 or 2 or 4 and for state 3.



Hope that helps.


Mandar


Hi mandar,



I already did that and alerts for the all the state with correct value but still the comment is not mandatory.