0

My scenario:

  • I have two fields, each of which is tied to third field's picklist values.
  • Only one of those two fields should display, depending on the selected picklist value.
  • Whichever of those two fields is displaying should be required.

And, the requirement causing the problem:

  • If one required field is displaying, and then the picklist is changed again, the required field should both stop displaying and being required without any value having to be entered into it first.

Here is the picklist field with an actionSupport to rerender

<apex:inputField value="{!Case.Picklist__c}" required="True">
     <apex:actionSupport event="onchange" reRender="fieldBlock"/>
</apex:inputField>

And here is the pageblocksection with the conditionally required and rendered fields:

<apex:outputPanel id="fieldBlock">
    <apex:pageBlockSection>
         <apex:inputField value="{!Case.field1__c}" 
                          rendered="{!Case.Picklist__c=='Option 1'}"
                          required="{!Case.Picklist__c=='Option 1'}"/>
         <apex:inputField value="{!Case.field2__c}" 
                          rendered="{!Case.Picklist__c=='Option 2'}"
                          required="{!Case.Picklist__c=='Option 2'}"/>  
    </apex:pageBlockSection>
</apex:outputPanel>

Things I've tried from other posts on this topic:

  • Using immediate = true attribute on the actionSupport doesn't cause fieldBlock to rerender.
  • Using actionRegion tags doesn't seem to address the core issue that the region I want to rerender has required fields.

I believe one possible solution would be to remove the required attributes from the VF and instead use a controller method or javascript to validate the required fields. But before I turn to that I'd like to better understand why VF can't handle what I'm trying to accomplish: I've read that rerendering submits the entire page to the server, but shouldn't that not be an issue with my conditional logic for required fields?

2 Answers 2

2

When form contains mandatory fields immediate="true" will not work for ActionSupport.

Secondly, when form has mandatory fields, as per execution logic setters will fire first and then action methods are getting called. Though here you haven't used to call to controller. So, unfilled mandatory fields will restrict actionSupport to perform.

Recommendation:

1) If you want to use actionSupport then remove immediate=true and remove required=true for all the mandatory fields. You can perform validation at controller level.

2) If you really want to use required attribute then use actionFunction instead of actionSupport.

You can refer my answer of one of this kind of scenarios ReRendered form with wrong data on inputFields dependent picklist

2
  • Hi Santanu, thanks for your response. From my original post: I believe one possible solution would be to remove the required attributes from the VF and instead use a controller method or javascript to validate the required fields. But before I turn to that I'd like to better understand why VF can't handle what I'm trying to accomplish. It seems both your recommendations are about using controller methods instead, but I'm interested in understanding why my original code isn't working. actionFunction isn't really different from just using the controller to validate.
    – smohyee
    Feb 22, 2017 at 22:35
  • I have already answered, when actionSupport is used with mandatory blank field values then setters for mandatory fields will restrict the processing Feb 22, 2017 at 22:45
0

If you wrap your picklist inside an apex:actionRegion it should work. I tried this code below and it is working for me.

<apex:page standardController="Book__c" >
    <apex:form id ="myform">
        <apex:actionRegion>
            <apex:inputField value="{!Book__c.Country__c}" required="true" >
                <apex:actionSupport event="onchange" reRender="fieldBlock" />
            </apex:inputField>
        </apex:actionRegion>
        <apex:outputPanel id="fieldBlock">
            Phone: <apex:inputField value="{!Book__c.My_Phone__c}" rendered="{!IF(CONTAINS(Book__c.Country__c,"INDIA"),"true","false")}" required="{!IF(CONTAINS(Book__c.Country__c,"INDIA"),"true","false")}"/>
            Email: <apex:inputField value="{!Book__c.My_Email__c}" rendered="{!IF(CONTAINS(Book__c.Country__c,"USA"),"true","false")}" required="{!IF(CONTAINS(Book__c.Country__c,"USA"),"true","false")}"/>
        </apex:outputPanel>
        <apex:commandButton value="Save" action="{!Save}" />
    </apex:form>
</apex:page>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .