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
Semira@gmail.comSemira@gmail.com 

Make a field required when a checkbox is not checked through VF Page

Hi I have a visualforce page where I made a input check box. I wan't to make another field required when this checkbox is NOT checked. 

Here's the part of VF code: 

<apex:pageblockSection id="ProjectInfo2" title="Project Site Contact Information" collapsible="true" Columns="2">
                <apex:inputField value="{!job.Project_Site_Contact_Name__c}"/>
                <apex:inputCheckbox label="Same as caller" onclick="{!SameAsCaller}"/>
                <apex:commandButton onclick="window.open('/apex/NewRecord','Popup','height=400,width=1000,left=100,top=100,scrollbars=yes,toolbar=no,status=no')" value="New Project Site Contact"/>
            </apex:pageblockSection>


Now I want to use something like:

<apex:inputField value="{!job.Project_Site_Contact_Name__c}" required='{!IF((!SameAsCaller == 'false'), 0, 1)}'/>

However, this is saying I'm providing text instead of boolean value. Is this even possible, if so how?

Please HELP! 
James LoghryJames Loghry
The required, rendered, and other boolean type attributes are easily confused when you start using exclamation marks and other functionality.  Try using the following instead:

<apex:inputField value="{!job.Project_Site_Contact_Name__c}" required='{!IF(SameAsCaller,false,true)}'/>

What this says is "require the field when SameAsCaller is false".  Alternatively, you could use required="{!NOT(SameAsCaller)}"

Hope this helps.
Semira@gmail.comSemira@gmail.com
I tried both of them. It doesn't give me anymore error however, fuctionality doesn't work. It is either making it required or not required, regardless of the checkbox.
Guna sekaranGuna sekaran
Try this 

https://developer.salesforce.com/forums?id=906F000000096T8IAI

<apex:inputField value="{job.Project_Site_Contact_Name__c}" required="{!IF(SameAsCaller,'true'), true, false)}"  />



David "w00t!" LiuDavid "w00t!" Liu
Non-code solution: create a validation rule that says your field must be populated if the checkbox is not checked. Make sure to have the pageMessages tag somewhere on your page.  This way is easy but it will NOT mark the field red (although users will get an error when trying to bypass it)

Code solution: have an actionFunction that rerenders your required field when the checkbox is unchecked and shows the field/makes it required. This way you get the cleanest UI solution.
Semira@gmail.comSemira@gmail.com
Thank you @David "w00t!" Liu, never worked with actionFuction. However, my checkbox doesn't exit on the object. It's a field I created on VF page. So non-code solution won't work. I'm trying Not to create more custome fields. 

Code solution: When I put is action:{!SameAsCaller} and try to save it, it is asking to to create a method. I SameAsCaller is a boolean variable that's only taking the input whether it's true or not. How to get around it.