You need to sign in to do that
Don't have an account?

How to add a PageMessage for a required field in a datatable?
I think this is a common problem, but I'm unable to get the 'right' answer for it throughout forums and elsewhere. I have a datatable and want to display the error message for a required selectlist next to the field in the table if its not entered:
<apex:pageBlockTable value="{!MyElements}" var="element" id="myTable">
<apex:column >
<apex:facet name="header">Request To</apex:facet>
<apex:outputfield value="{!element.element.Function__c}" id="function"/>
</apex:column>
<apex:column >
<apex:facet name="header">Requested By</apex:facet>
<apex:outputfield value="{!element.element.CreatedBy.Name}" id="createdby"/>
</apex:column>
<apex:column >
<apex:facet name="header">Request Date</apex:facet>
<apex:outputfield value="{!element.element.CreatedDate}" id="createddate"/>
</apex:column>
<apex:column >
<apex:facet name="header">Action</apex:facet>
<apex:selectList value="{!element.approvalOption}" size="1" id="action" required="true">
<apex:selectoptions value="{!ApprovalOptions}"/>
</apex:selectList>
</apex:column>
<apex:column >
<apex:facet name="header">Transfer To</apex:facet>
<apex:selectList value="{!element.transferTo}" size="1" id="transferTo" required="true" rendered="{!element.showTransferOptions}">
<apex:selectoptions value="{!transferOptions}"/>
</apex:selectList>
<apex:message id="transferToMessage" for="transferTo"/>
<!-- or <apex:pageMessage id="transferRequired" severity="2"/>-->
</apex:column>
</apex:pageBlockTable>
Can someone guide how to get a reference to the message or page message if the select option is not selected? Right now, I have it as required, so I get the error in the page messages on the top of the page as but with the dom reference to the missing fields :
mypage:myForm:myBlock:myTable:4:transferTo: Validation Error: Value is required
mypage:myForm:myBlock:myTable:5:transferTo: Validation Error: Value is required
Thanks,
-Manu
You just need to do your check to see if the field is null and then add an error if it is. Something like this:
Object.Custom_Field__c.addError('Your error message.');