+ Start a Discussion
Krishna Prasad K PKrishna Prasad K P 

How to show Error message near an inputField using custom controller

I have made a Visualforce page and custom controller for mass adding Sales Team Members for  Opportunities.
I used a newly created list of 5 OpportunityTeamMember records(to be inserted) for iteratively dispalying 5 input sets as in the standard Add Sales Team Page.
I wish to have a validation rule which, when the "Save" button is clicked, should add an error message near the TeamRole field of the if it is null.
I tried by the code

<apex:pageBlockTable value="{!OpportunityTeamMemberList}" var="TeamMember">
<apex:column headerValue="Team Role">                                           
   <apex:inputField id="inputTeamRole" value="{!TeamMember.TeamMemberRole}" />

        MyTeamMemberList[index].TeamMemberRole.addError('You must enter a value'); 

Problem with this code is that, if the user skips the TeamRole for the second or third input row, the message shows near the Team Role input field in the FIRST input row.
How can I add an Error message near theTeam Role inputField of the REQUIRED input row?
Can I use ApexPages.addMessage to input component of the SPECIFIC input row?
Thanks in advance
Hi Krishna,

If all you are trying to do is make an inputField required and then "when the 'Save' button is clicked...add an error message near the TeamRole field of the if it is null" there's a built-in Visualforce attribute for that called required. You don't need to do anything in your controller. Just do this:
<apex:inputField id="inputTeamRole" value="{!TeamMember.TeamMemberRole}" required="true" />

This should do exactly what you want, it does what you describe in the pages where I use it.

Also, if you want a list of all the errors on the page to be shown somewhere on the
page, e.g. a summary at the top, you can use the <apex:pageMessages /> tag.


Message Edited by ptepper on 09-02-2008 03:43 PM
Krishna Prasad K PKrishna Prasad K P
Hi Ptepper,
I have tried this too. But if I give "required" attribute "True", Team Role field in all the input rows will show as required.(with a red line near the input box). In that case user will HAVE TO fill all the 5 team member input rows before clicking "Save". He/she cannot save with only one team member entry.
In the standard Add Sales team page, user can enter 5 or less number of team members at a time. So in my custom page also I wish to have this freedom.
I tried the <apex:pageMessages /> too, but this shows the message at a common place, But i wish to have it near the Team Role field (with null value)  of the specific input row.(just like in the standard Add Sales Team page).
I dont understand why the AddError shows message always in the first input row !?
Please help me to find a solution.
I don't know a straightforward way to do what you're talking about using the Apex error reporting functionality. It might be possible though.

You could do a workaround, using some variables to store individual error messages and displaying them as outputFields, rendering them only if the vars are set with error messages.

Hi Krishna,
Were you able to show the error at the right position. If yes, it would be great if you could post the solution in this thread.
If not, can anyone else help us find the solution.
The problem is:
Whenever we are iterating through an object and displaying its properties in a page block table and if there is a validation error, then I want that error to show up just below that field.
Currently, I am using addError() on a particular field in the controller and inputField in the page. However, the page always displays the error on the first row irrespective of which row the error has taken place.


<apex:pageBlockTable id="orderTable" value="{!orderItems}" var="orderItem" styleClass="list" headerClass="headerRow">
              <apex:facet name="header">
                <apex:outputText value="Field1"/>
              <apex:inputField Id="field1" value="{!obj1.field1}" required="false"/>


Hi Krishna,


I was trying for the same thing, But i was not able to get the error message in the page block table, can you help me how were you able to get a error message on the field used/dispayed in pageblocktable.


Thanks much in advance,



Has anybody found a solution?


To paraphrase, the question is:  When you enable editing through a list controller, how do you get any on-save error messages to be displayed only on the record(s) that have an error?  The <apex:message> and <apex:messages> tags do not appear to work within a <apex:pageBlockTable> tag for this purpose.


Unless there is a solution, then editing in lists is essentially unusable, because there's no way to provide a user with feedback about which specific row triggered an error.   


This is related to the following two pages of documentation.


Visualforce Developer's Guide, version 24.0