1

I have a picklist with certain values that make another field required. I am trying to get the field required value to rerender when the picklist value changes, but no luck so far. I have tried adding a blank action to the actionSupport tag to no avail.

Page Snippet:

<apex:inputField value="{!someAccount.someField}">
    <apex:actionSupport event="onchange" reRender="otherFieldRequired" />
</apex:inputField>
<apex:pageBlockSectionItem id="otherFieldRequired">
    <apex:outputLabel value="Other Field Required?" />
    <apex:outputText value="{!IF(isOtherFieldRequired, 'Yes', 'No')}" />
</apex:pageBlockSectionItem>

Controller Snippet:

public static Set<String> requiredPicklistValues = new Set<String> { 'A', 'B', 'C' };
public Account someAccount {get; set;}
public Boolean isOtherFieldRequired { get { return requiredPicklistValues.contains(someAccount.someField); } }
1
  • Did you try enclosing the pageblock inside of an outputPanel with the id as otherFieldRequired? I know it is usually a pain in the keister about that. Aug 5 '13 at 21:25
2

Just need to create a property in the controller to let the page know whether or not the field is required. You set that property int he action of the actionSupport, and then rerender.

VF Page

<apex:inputField value="{!someAccount.someField}">
     <apex:actionSupport event="onchange" action="{!checkRequired}" reRender="otherFieldRequired" />
</apex:inputField>
<apex:outputPanel id="otherFieldRequired">
     <apex:inputField id="dependentField" required="{!isRequired}"/>
</apex:outputPanel>

Controller

public boolean isRequired  {get;set;}{isRequired=false;}

public PageReference checkRequired(){
     //Your logic to check if the appropriate pick list values have been set for the input field
     if(//your check){
          isRequired = true;
     }
}
9
  • Adrian, did you try this? Aug 26 '13 at 1:04
  • I have similar problem. I can't make second field not required if I set the first value to value different from the list.
    – Patlatus
    Sep 22 '14 at 9:08
  • 1
    It seems it is impossible to make field "Not required" dynamically once it was marked as "required". Dumb visualforce
    – Patlatus
    Sep 22 '14 at 9:18
  • @Patlatus Yep it seems once required, always required. Unfortunate.
    – Adrian Larson
    Oct 30 '15 at 18:11
  • Actually I have been playing with javascript, trying to mark field as required or unmark. Also I am thinking now, if Chris example might work in case if we surround first <apex:inputField> with <apex:actionRegion> tag
    – Patlatus
    Nov 1 '15 at 7:05

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.