You need to sign in to do that
Don't have an account?
Cable Guy.ax375
Conditionally make field required and rendered
I want to implement the following logic:
If A == 'Off', then don't display inputField;
if A == 'On', then display inputField but make inputField not required
If A=='On/Required', then display inputField and make inputField required
I have the following code:
<apex:inputField value="{!B}" required="{IF(A =='On/Required', true, false)}" rendered="{!A != 'Off'}" />
It doesn't work. Can anyone point out where it went wrong?
Thanks in advance,
Maybe this:
<apex:inputField value="{!B}" required="{!IF(CONTAINS(A,'On/Required'), true, false)}" rendered="{!IF(NOT(CONTAINS(A,'Off')),true,false)}" />
All Answers
<apex:inputField value="{!B}" required="{!IF(A =='On/Required', true, false)}" rendered="{!IF(A != 'Off',true,false)}" />
Here is the reference of using IF Function in Visualforce:
http://www.salesforce.com/us/developer/docs/pages/Content/pages_variables_functions.htm?SearchType=Stem&Highlight=if|If|IF
Thanks a lot. I implemented the code but it doesn't work. I suspect the string comparison doesn't work.
Maybe this:
<apex:inputField value="{!B}" required="{!IF(CONTAINS(A,'On/Required'), true, false)}" rendered="{!IF(NOT(CONTAINS(A,'Off')),true,false)}" />
Thanks again. It still doesn't work. If I hardcode true or false, then it works. So it's something wrong in the if statement. But it's pretty straightforward.
It works once I changed A's datatype to picklist. It used to be lookup field. Anyway, the syntax works perfectly. Thanks again.
However, when I try to change it back to 'Off' without filling in the inputField, it'll throw an error demanding a value be inputted. It seems that visualforce checks on if the field is rendered before it checks on if it's required, and refuses to unrender it if it thinks it's still required.