Question

I'm using an inputField that is binded directly to a custom objects field in the controller. The following will generate a dropdown list with a label.

 <apex:inputField value="{!Agency_Profile.Location_Principal_Activity__c}" />

My problem is that I need to add the required mark next to the inputField without losing the label or having default error msgs.

when I used

<apex:inputField value="{!Agency_Profile.Location_Principal_Activity__c}" required="true"/>

I got the required mark but I lost my custom error msgs for validation.

when I used

<apex:outputPanel styleClass="requiredInput" layout="block">
     <apex:outputPanel styleClass="requiredBlock" layout="block"/> 
     <apex:inputField value="{!Agency_Profile.Location_Principal_Activity__c}" />
  </apex:outputPanel>

the labels near the dropdown list didnt show anymore..

Is there a way I can accomplish what I need?

Was it helpful?

Solution

I ended up using this.

//this part to add the missing label.
<apex:outputLabel 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:inputField value="{!Agency_Profile.Location_Principal_Activity__c}" />
  </apex:outputPanel>

OTHER TIPS

The best way is to add validation rule for this field for this object.

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)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top