I have a custom object with a field named AnswerType that is a Picklist, and has possible values of Picklist, Text or Textarea. Essentially, the client wants to be able to ask the user a question in a form and have the answer be either a select list or a textbox/textarea. I need to be able to dynamically determine which type of input to present based on the AnswerType. I don't think I can use apex:inputField because that only works with standard objects and this is a custom object. If I can use an apex:inputField I don't know a) what type of variable to use in my controller for b) the value of the apex:inputField. If I could, in my controller I would have a variable, such as:

Public InputField question;

Then in Visualforce:

<apex:inputField value="{!question}"/>

and let VisualForce determine which type of input to render.

I hope I'm explaining this well. Does anyone know how I can accomplish this? I'd rather not have to put selectLists and textboxes in my Visualforce page, then render only the one I need. But if that's my only option, I'll do it.

Thanks for any advice anyone has.

  • 1
    inputFields need to be bound to sobject Fields; no dynamic value=".." attribute possible; three fields with rendered= will do the trick
    – cropredy
    Mar 25, 2016 at 22:02

1 Answer 1


Depending on your question form's complexity, it could be challenging to use an <apex:inputField /> tag.

Assuming the custom object you refer to with the field on it of AnswerType is the Question, you could use something like this in the VF page to handle each type of display output you intend to support.

In this model, each Question record has the text of the question and the DisplayType__c for driving the UI element shown to gather the response. The Answer record is very simple and just has a Long Text Area field on it that captures any answer type in the exact same field, as plain text.

Note: items not shown - getting the list of questions, storing the answer related to the question, or getting the selectOptions if the display type is a picklist. Wrapper classes are used very heavily in the real-life version of the simplified markup below.

<!-- If the question has been defined as a radio button -->
<apex:outputPanel layout="none" id="radioPanel" rendered="{!Question.DisplayType__c == 'Radio'}">
    <apex:selectRadio id="selectRadioInput" value="{!Answer.AnswerValue__c}">
        <apex:selectOptions value="{!SelectOptions}" />

<!-- If the question has been defined as text input -->
<apex:outputPanel layout="none" id="textPanel" rendered="{!(Question.DisplayType__c == 'Text')}">
    <apex:inputText id="textInput" value="{!Answer.AnswerValue__c}" />

<!-- If the question has been defined as textArea input -->
<apex:outputPanel layout="none" id="textPanel" rendered="{!(Question.DisplayType__c == 'Text Area')}">
    <apex:inputTextArea id="textAreaInput" rows="3" value="{!Answer.AnswerValue__c}"  />

<!-- If the question has been defined as a checkbox input -->
<apex:outputPanel layout="none" id="checkboxPanel" rendered="{!Question.DisplayType__c == 'Checkbox'}">
    <apex:inputCheckbox id="checkboxInput" value="{!Answer.AnswerValue__c}" />

<!-- If the question has been defined as a single picklist -->
<apex:outputPanel layout="none" id="picklistPanel" rendered="{!(Question.DisplayType__c == 'Picklist')}" >
    <apex:selectList id="selectListInput" value="{!Answer.AnswerValue__c}" multiselect="false" size="1" >
        <apex:selectOptions value="{!SelectOptions}" />

<!-- If the question has been defined as a multiselect picklist -->
<apex:outputPanel layout="none" id="multiPicklistPanel" rendered="{!(Question.DisplayType__c == 'Picklist (Multi-Select)')}" >
    <apex:selectList id="multiPicklist" value="{!Answer.AnswerValue__c}" multiselect="true" size="5">
        <apex:selectOptions value="{!SelectOptions}" />
  • 1
    and one could stick this all into a component so the main VF page didn't get visually overwhelmed in markup
    – cropredy
    Mar 25, 2016 at 22:56
  • Thanks for the answer, Mark. It is kind of what I suspected, so I will definitely take your recommended approach.
    – Steve
    Mar 28, 2016 at 12:24

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.