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
SabrentSabrent 

Required Field's Red Bar

If Book_Category__c == 'Fiction', I want the text field Book_Code__c to be required else not required.

How can I make the red bar appear and disappear?

 

I think i am missing the render , can someone please help?

 

 

 

<apex:page standardController="Book__c">
<apex:form>
<apex:pageBlock >

    <apex:pageBlockSection>
        <apex:pageBlockSectionItem>
            <apex:outputPanel>
                  <apex:outputLabel value="Book Category" />
                  <apex:inputField value="{!Book__c.Book_Category__c}"/>
            </apex:outputPanel>
        </apex:pageBlockSectionItem>
    </apex:pageBlockSection>


  
  <apex:pageBlockSection>
        <apex:pageBlockSectionItem>
            <apex:outputPanel>
                <div class="requiredInput">
                    <div class="requiredBlock"></div>
                    <apex:outputLabel value="Cost Code" />
                  <apex:inputField value="{!Book__c.Cost_Code__c}" required="{!IF(Book__c.Book_Category__c =='Fiction', true, false)}" />
                </div>
            </apex:outputPanel>
        </apex:pageBlockSectionItem>
    </apex:pageBlockSection>
</apex:pageBlock>
</apex:form>

</apex:page>

 

Best Answer chosen by Admin (Salesforce Developers) 
Jerun JoseJerun Jose

I think the main problem in your case, is that your second section has the div structure simulating SFDC's native required block.

 

So even if the required attribute has false, because your other divs are already displaying it as a required block, it will always show up as a required block.

 

I just created a working sample here based on the account object. Modify it to work on your page.

Also, there were some unnecessary actionsupports and divs, outputpanels. I have taken them off. Put them back if you really need them.

 

<apex:page standardController="Account">
    <apex:form >
    <apex:actionfunction name="rerenderOrderNum" rerender="orderNumberBlock" status="Mystatus"/>
        <apex:pageBlock mode="edit" title="Create New Employee">
        
           <apex:pageblockSection columns="3" title="Select Job Family">
              <apex:pageBlockSectionItem >   
                <apex:outputLabel value="Account Name" for="jf"/>
                <apex:outputPanel >
                    <apex:inputfield value="{!account.Name}" id="jf" onchange="rerenderOrderNum()"/> 
                    <apex:actionStatus startText="applying selected value..." id="Mystatus"/>
                </apex:outputPanel>
              </apex:pageBlockSectionItem> 
           </apex:pageblockSection>
     
    <apex:pageBlockSection columns="2" id="orderNumberBlock" >   
        <apex:pageBlockSectionItem >
            <apex:outputLabel value="Account Site" for="ksno"/> 
            <apex:inputField value="{!account.Site}" id="ksno" required="{!CONTAINS(account.Name,'Test')}" />
      </apex:pageBlockSectionItem>
    </apex:pageBlockSection>
    
    </apex:pageBlock>
    </apex:form>
</apex:page>

 

All Answers

Jerun JoseJerun Jose

You guessed right.

 

<apex:page standardController="Book__c">
<apex:form>
<apex:actionfunction name="rerenderCost" rerender="costCodeBlock"/>
<apex:pageBlock >
    <apex:pageBlockSection>
        <apex:pageBlockSectionItem>
            <apex:outputPanel>
                  <apex:outputLabel value="Book Category" />
                  <apex:inputField value="{!Book__c.Book_Category__c}" onchange="rerenderCost()"/>
            </apex:outputPanel>
        </apex:pageBlockSectionItem>
    </apex:pageBlockSection>


  
  <apex:pageBlockSection id="costCodeBlock">
        <apex:pageBlockSectionItem>
            <apex:outputPanel>
                <div class="requiredInput">
                    <div class="requiredBlock"></div>
                    <apex:outputLabel value="Cost Code" />
                  <apex:inputField value="{!Book__c.Cost_Code__c}" required="{!IF(Book__c.Book_Category__c =='Fiction', true, false)}" />
                </div>
            </apex:outputPanel>
        </apex:pageBlockSectionItem>
    </apex:pageBlockSection>
</apex:pageBlock>
</apex:form>

</apex:page>

 Used an actionfunction to call the rerender.

SabrentSabrent

Thanks for the response jerun .

 

I tried as you suggested but the required field's Red bar still shows..

 

This is a code snippet of my actual code. Can you please check ?  

 

<apex:actionfunction name="rerenderOrderNum" rerender="orderNumberBlock"/>
        <apex:pageBlock mode="edit" title="Create New Employee" id="pb" >
        
           <apex:pageblockSection columns="3" title="Select Job Family" id="jobFamily">
              <apex:pageBlockSectionItem >   
                <apex:outputLabel value="Job Family" for="jf"/>
                <apex:outputPanel >
                    <apex:inputfield value="{!ctEmpl.Job_Family__c}" id="jfa" onchange="rerenderOrderNum"> 
                    <apex:actionSupport event="onchange" rerender="pb" status="status"/>
                    </apex:inputField>
                    <apex:actionStatus startText="applying selected value..." id="status"/>
                </apex:outputPanel>
              </apex:pageBlockSectionItem> 
           </apex:pageblockSection>
     
     <apex:pageBlockSection columns="2" id="pbs" >   
      <apex:pageBlockSectionItem id="pbsiKsn" >  
                    <apex:outputLabel value="KSN Order No" for="ksno"/> 
                       <apex:outputPanel layout="block" >
                        <div class="requiredInput">
                        <div class="requiredBlock"></div>
                        <apex:inputField value="{!ctEmpl.KSN_Order_Number__c}" id="ksno" required="{!IF(CONTAINS(ctEmpl.Job_Family__r.Name,'KES Nursing'),true,false)}" />
                        </div>
                       </apex:outputPanel>
                  </apex:pageBlockSectionItem>
      </apex:pageBlockSection>

 

Ashish_SFDCAshish_SFDC

Hi Rov, 

 

A simple Validation Rule can solve this requirement. 

Criteria: AND(Book_Category__c == 'Fiction', Book_Code__c==Null)

Validation Error: Based on your requirement. 

 

Regards,

Ashish

SabrentSabrent

There's a reason why i don't validation rule, but want to dynamically control the field to be required or not in the visualforce page.

 

With validation rule, functionally it works correct however visually it's confusing to the users because the red bar still shows up whether the Book_Category__c (which is a lookup field)  is Fiction or any thing other than fiction.

 

e.g. If User Selects Fiction in the BookCategory the validation rule kicks and they are not required to put any value, however since the red bar still stays, users believe since it is a required field they have to put something but don't know what, and, as a result end up getting frusttrated.For every other value selected in the Book_Category, they know what to put in the Cost_Code__c.

 

SabrentSabrent

I wonder if  it the 'Contains' keyword... (since Job_Family__c is a lookup field ) that's incorrect 

 

<apex:inputField value="{!ctEmpl.KSN_Order_Number__c}" id="ksno" required="{!IF(CONTAINS(ctEmpl.Job_Family__r.Name,'KES Nursing'),true,false)}" />

Jerun JoseJerun Jose

I think the main problem in your case, is that your second section has the div structure simulating SFDC's native required block.

 

So even if the required attribute has false, because your other divs are already displaying it as a required block, it will always show up as a required block.

 

I just created a working sample here based on the account object. Modify it to work on your page.

Also, there were some unnecessary actionsupports and divs, outputpanels. I have taken them off. Put them back if you really need them.

 

<apex:page standardController="Account">
    <apex:form >
    <apex:actionfunction name="rerenderOrderNum" rerender="orderNumberBlock" status="Mystatus"/>
        <apex:pageBlock mode="edit" title="Create New Employee">
        
           <apex:pageblockSection columns="3" title="Select Job Family">
              <apex:pageBlockSectionItem >   
                <apex:outputLabel value="Account Name" for="jf"/>
                <apex:outputPanel >
                    <apex:inputfield value="{!account.Name}" id="jf" onchange="rerenderOrderNum()"/> 
                    <apex:actionStatus startText="applying selected value..." id="Mystatus"/>
                </apex:outputPanel>
              </apex:pageBlockSectionItem> 
           </apex:pageblockSection>
     
    <apex:pageBlockSection columns="2" id="orderNumberBlock" >   
        <apex:pageBlockSectionItem >
            <apex:outputLabel value="Account Site" for="ksno"/> 
            <apex:inputField value="{!account.Site}" id="ksno" required="{!CONTAINS(account.Name,'Test')}" />
      </apex:pageBlockSectionItem>
    </apex:pageBlockSection>
    
    </apex:pageBlock>
    </apex:form>
</apex:page>

 

This was selected as the best answer
SabrentSabrent
Thank you!! Appreciate your patience.