3

I want to build page with dynamic input type. I want vf code to look like this:

<apex:repeat value="{!myData}" var="myEntry">
    <apex:pageBlockSection>
        <apex:pageBlockSectionItem>
            <apex:outputText value="{!myEntry.label}"/>
            <apex:input value="{!myEntry.value}" type="{!myEntry.type}"/>
        </apex:pageBlockSectionItem>
    </apex:pageBlockSection>
</apex:repeat>

For this I have apex code like this:

public class MyEntry {
    public MyEntry(String label, String key, String value, String type) {
        this.label = label;
        this.key = key;
        this.value = value;
        this.type = type;
    }
    public String label { get; set; }
    public String key { get; set; }
    public String value { get; set; }
    public String type { get; set; }
}

public List<MyEntry> MyData {
    get {
        if (MyData == null) {
            MyData = new List<MyEntry>();
            MyData.add(new MyEntry('Some date', 'Some_Date__c', null, 'date'));
            MyData.add(new MyEntry('Some checkbox', 'Some_Checkbox__c', null, 'checkbox'));
            MyData.add(new MyEntry('Some text', 'Some_Text__c', null, 'text'));
        }
        return MyData;
    }
    set;
}

But when I use type="{!myEntry.type}" I get error:

Error:(1, 1) Literal value is required for attribute id in <apex:input>

And for html-type="{!myEntry.type}" I get this:

Error:(1, 1) Cannot override attribute 'type' on component <apex:input>

Is there any solution for my problem?

0

1 Answer 1

4

Unfortunately, you have use a particularly verbose syntax:

         <apex:input rendered="{!myEntry.type='text'}" value="{!myEntry.value}" type="text"/>
         <apex:input rendered="{!myEntry.type='date'}" value="{!myEntry.value}" type="date"/>
         <apex:input rendered="{!myEntry.type='number'}" value="{!myEntry.value}" type="number"/>
         <apex:input rendered="{!myEntry.type='checkbox'}" value="{!myEntry.value}" type="checkbox"/>
         <!-- more if you need them -->

This is required because VF requires a hard-coded type.

2
  • 3
    But you could at least move it to a component so you don't have to repeat that ad nauseum.
    – Adrian Larson
    Sep 16, 2016 at 17:25
  • for checkbox have to use apex:inputCheckbox Sep 16, 2016 at 21:41

You must log in to answer this question.

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