I'm looking for a way to simplify my 'Required' parameter on my VF page fields.

For example, I want my 'Account' field to be required if one of a half dozen 'Activity' options are selected.

Currently, I have to write out the comparison to each possible value within the visualforce parameter itself, eg:

<apex:inputfield value="{!entry.Account__c}" required="{!(entry.Activity__c == 'option 1' || entry.Activity__c == 'option 2' || ... etc)}" />

This is very cumbersome even with evaluating only a single field's options... imagine if it had to be determined from the combined values of multiple fields!

So now I'm wondering: Is it possible to compare a parameter value to a LIST of values?

Say in my controller I defined a list of strings of Activity field options. Could I do something SOQL-ish like this?:

<apex:inputfield value="{!entry.Account__c}" required="{!(entry.Activity__c IN :valueListFromController)}" />

Just use a Set whitelist and add a Boolean property.

static final Set<String> whitelist = new Set<String> { 'option 1', 'option 2', 'etc.' };
public Boolean isAccountRequired { get { return whitelist.contains(entry.Activity__c); } }

Then in your markup, simply reference this flag property:

<apex:inputField value="{!entry.Account__c}" required="{!isAccountRequired}" />

This approach will scale well enough when adding more display logic, either new fields, new whitelists, or both.

If entry is an item in a collection, you have a couple options. The most straightforward is to use a wrapper class:

public class EntryWrapper
    public Boolean isAccountRequired { get; private set; }
    public MyObject__c record { get; private set; }
    public EntryWrapper(MyObject__c record)
        this.record = record;
        this.isAccountRequired = whitelist.contains(record.Activity__c);

Then your markup would change along the lines of:

<apex:repeat value="{!wrappers}" var="wrapper">
    <apex:inputField value="{!wrapper.record.Account__c}"
        required="{!wrapper.isAccountRequired}" />
  • Thanks! Will this work if 'entry' is one of several line-item records listed with the page's record (each with its own Account and Activity)? Will the boolean return different values for each entry record? I figure it wont since you cant pass variables through parameter methods.
    – smohyee
    Nov 22 '16 at 4:35
  • 1
    @smohyee in that case I would go with a wrapper class.
    – Adrian Larson
    Nov 22 '16 at 22:59

You can do the comparison on controller side and display the required attribute value based on the outcome.

I have written a small snippet like this where based on the picklist selection inputField can be mandatory or optional. The evaluateRequiredValue() method is making decision for isRequired attribute:


<apex:page standardController="Opportunity" extensions="ExcelPageController">
    <apex:form >
        <apex:outputPanel id="vendor2">
            <apex:selectList value="{!vendor}" multiselect="False" size="1">
                <apex:actionSupport event="onchange" rerender="otherPanel" action="{!evaluateRequiredValue}"/>
                <apex:selectOptions value="{!vendors}"/>
        <apex:outputPanel id="otherPanel">                 
             <apex:inputField value="{!opptyObj.LeadSource}" required="{!isRequired}"/>                



public with sharing class ExcelPageController {
    public String vendor {get; set;}//Vendor Name
    public Boolean isRequired {get;set;}

    public Opportunity opptyObj{get;set;}
    ApexPages.standardController stdController = null;
    public ExcelPageController(ApexPages.StandardController controller) {
        this.stdController = controller;

    public List<SelectOption> getVendors(){
         List<SelectOption> options = new List<SelectOption>();
         options.add(new SelectOption('Cisco','Cisco'));
         options.add(new SelectOption('Alcatel','Alcatel'));
         options.add(new SelectOption('Tibco','Tibco'));
         options.add(new SelectOption('Other','Other'));         
         return options;

    public void evaluateRequiredValue() {       
        if(vendor.equals('Cisco') || vendor.equals('Tibco')) {
            isRequired = true;
        }else {        
            isRequired = false;

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.