[SalesForce] Assign Ids dynamically to Apex:inputField

I'm trying to assign Id to my inputField dynamically, those input fields are coming from field sets.
These Ids needs to be assigned to those fields which are required in database.

On giving Id dynamically I'm getting Literal value is required for attribute id in error.

Based on those ids I need to perform some check in javascript, before saving the record.

<apex:outputPanel id="mySalesforceFields">
  <apex:repeat value="{!fieldSetMemberList}" var="field">
    <apex:inputField value="{!sObject[field]}" required="{!if((field.dbRequired || field.required), true,false)}" 
    id="{!if((field.dbRequired || field.required), field.label,'')}" />

  </apex:repeat>
</apex:outputPanel>

Best Answer

You can't dynamically assign the Visualforce id attribute. You can, however, assign data- attributes or pass through html- attributes. See, for example, Setting Custom HTML Attributes on Visualforce Components . Make sure you use a unique value such as the field's API Name rather than its Label if you decide to pass through an html-id.

Probably better to use data- attributes. You can still select on them.

<apex:inputField ... html-data-id="{!field}"
    html-data-required="{!OR(field.required, field.dbRequired)}" />
Related Topic