using raym0nds approach, this is how it looks for a custom controller variable, in my case with the name from a custom field of an object.:
//this part to add the missing label.
<apex:outputLabel for="myId" styleclass="labelCol" value="{!$ObjectType.Agency_Profile__c.fields.Location_Principal_Activity__c.Label}" />
<apex:outputPanel styleClass="requiredInput" layout="block">
<apex:outputPanel styleClass="requiredBlock" layout="block"/>
<apex:inputText id="myId" required="true" value="{!myCustomField}" label="{!$ObjectType.Agency_Profile__c.fields.Location_Principal_Activity__c.Label}" />
</apex:outputPanel>
note the apex:inputText
type which now has an label
, id
and required
attribute. The apex:outputLabel
now has a for
attribute. The for/id
is just so clicking on the label will put the cursor into the right field. The required
enables form validation, because the rest is just make-up. The label
adds a good field name to that validation error - otherwise it would show the internal field id there.
the whole approach is interesting if you have a mass edit table in which all records share certain values (e.g. add multiple leads for the same company)