5

EDIT 3:

Just to make things easier I have used my custom object and here is what is happening:

First: When the page loads:

enter image description here

Second: When the checkbox is checked:

enter image description here

Third: When checkbox is Un-checked

enter image description here

END EDIT 3

EDIT 2: VFP screen shot: does not even render the checkbox:

<apex:page standardController="Opportunity" >
<apex:pageMessages />
<apex:form >
    <apex:pageblock >
        <apex:pageBlockSection title="prevent ..." columns="1" id="amount">    
            <apex:inputField value="{!Opportunity.isprivate}">
                <apex:actionSupport event="onchange" rerender="amount" />
            </apex:inputField>
            <apex:inputField required="{!Opportunity.isprivate}" value="{!Opportunity.amount}" id="amId"/>
        </apex:pageBlockSection>  
    </apex:pageblock>
    <apex:commandButton action="{!save}" value="Save" id="theButton"/>
</apex:form>
</apex:page>

enter image description here

END:

Here is the screen mockup will give you an idea:

Checked:

enter image description here

UnChecked:

enter image description here

I have Visualforce page and below is the code and what I want to have is conditional on/off of validation on the page, if the user clicks on checkbox then condition enabled otherwise disabled and do not fire.

so I have required= true on the description

Visualforce page:

 <apex:actionRegion >        
        <apex:pageBlockSection id="TSection" columns="2"  >                 
           <apex:inputField value="{!author__c.Is_author__c}">
             <apex:actionSupport event="onchange" rerender="TSection" />
           </apex:inputField>

           <apex:pageBlockSectionItem />               
              <apex:inputField required="true" value="{!author__c.Description__c}" rendered="{!author__c.Is_author__c}"/>
               <apex:inputField value="{!author__c.Audience__c}" rendered="{!author__c.Is_author__c}"/> 
        </apex:pageBlockSection>    
     </apex:actionRegion> 
  • At first required should be false, but when you check Is_author__c, required should become true. Is my understanding correct? – Vigneshwaran G May 11 '16 at 21:41
  • I have updated my question with screen shot to give you visualize – Nick Kahn May 11 '16 at 22:04
  • Does your user have access to isprivate field? goto any opp-ty and check it. Does it work for you custom object ? – Ilya Lepesh May 12 '16 at 5:54
  • @IlyaLepesh: check out my EDIT 3, I have demonstrate the problem and I'm using exact code you have pasted except that I'm using my custom object and field names different but the entire logic is the same. – Nick Kahn May 12 '16 at 12:53
  • @nick-kahn wrap checkbox with actionregion, answer is updated – Ilya Lepesh May 12 '16 at 20:31
7

To make inputfields required by checkbox value, provide attribute required="{!checkboxField}" for you fields. Add AJAX support within actionSupport to your checkbox, where you could rerender required fields.

Given page utilizes opportunity standardcontroller, where you already have checkbox field:

<apex:page standardController="Opportunity">
<apex:pageMessages />
<apex:form >
    <apex:pageblock >
        <apex:pageBlockSection columns="1" id="Section"> 
            <apex:pageBlockSectionItem>
                <apex:outputLabel for="bool" value="CheckBox" />
                <apex:actionRegion>
                    <apex:inputField value="{!Opportunity.isprivate}" id="bool">
                        <apex:actionSupport event="onchange" rerender="Section"/>
                    </apex:inputField> 
                </apex:actionRegion>
            </apex:pageBlockSectionItem>
            <apex:inputField required="{!Opportunity.isprivate}" 
                value="{!Opportunity.amount}"/>
        </apex:pageBlockSection> 
        <apex:pageBlockButtons location="bottom">
            <apex:commandButton action="{!save}" value="Save"/>
        </apex:pageBlockButtons>
    </apex:pageblock>
</apex:form>
</apex:page>

actionRegion will send only checkbox field to server during validation. Save will send whole form.

Demo:

enter image description here

  • 1
    Can't we put condition in required instead of two inputField with rendered condtions? – Vigneshwaran G May 11 '16 at 22:10
  • 1
    @VigneshwaranG: good point and I'm thinking the same – Nick Kahn May 11 '16 at 22:15
  • are you using any apex code? does not seems to work on my end and also the formatting is messed up as well, the way you have on GIF space mine have more then one textfield so the both together in tight spot – Nick Kahn May 11 '16 at 23:15
  • i'm not sure if this will effect or not but i'm using custom object, can you paste the entire vfp and apex code the sample you have? – Nick Kahn May 11 '16 at 23:26
  • @nick-kahn, please check updated section – Ilya Lepesh May 12 '16 at 0:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.