function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
md1md1 

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 

 

knicholsknichols

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.');