function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Cable Guy.ax375Cable 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,

Best Answer chosen by Admin (Salesforce Developers) 
CoryCowgillCoryCowgill

Maybe this:

 

<apex:inputField value="{!B}" required="{!IF(CONTAINS(A,'On/Required'), true, false)}" rendered="{!IF(NOT(CONTAINS(A,'Off')),true,false)}" />

All Answers

CoryCowgillCoryCowgill

<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

Cable Guy.ax375Cable Guy.ax375

Thanks a lot. I implemented the code but it doesn't work. I suspect the string comparison doesn't work.

CoryCowgillCoryCowgill

Maybe this:

 

<apex:inputField value="{!B}" required="{!IF(CONTAINS(A,'On/Required'), true, false)}" rendered="{!IF(NOT(CONTAINS(A,'Off')),true,false)}" />

This was selected as the best answer
Cable Guy.ax375Cable Guy.ax375

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.

Cable Guy.ax375Cable Guy.ax375

It works once I changed A's datatype to picklist. It used to be lookup field. Anyway, the syntax works perfectly. Thanks again.

David Kingery 3David Kingery 3
So, when I try this same thing, I change A to 'On/Required' and it shows the field with the red next to it indicating it's required.
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.