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
Guyver118Guyver118 

<apex:message> how to get it to work for specific fields?

Hi i was wondering could someone give me an example on how to get <apex:message> to work, for example i have field called contact name and want the error to be shown on that field.

 

if(!IsDuplicateLink()) { insert s; } else { Account dupe = [SELECT Id, Name FROM Account WHERE Id = :eId]; ApexPages.Message dupeMsg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Contact already exists in ' + dupe.Name); ApexPages.addMessage(dupeMsg); drawError = true; } <apex:message for="conName"/> <apex:inputField id="conName" value="{!Contact.Name}"/>

 

for some reason this does not work?

 

KeithJKeithJ

for (Account a : Trigger.new) { if (a.name == 'bad') { a.name.addError('Bad name'); //will add an error to the account NAME field } }

 

Guyver118Guyver118
hmm but will this automatically add the error to my custom visualforce page? I am also having problems where i want my standard validation rules which are shown most at the field level, to be shown in my custom visualforce page?
KeithJKeithJ

Ah, you want it to work for underlying validation rules...

That, I have not done before. I would be curious to see how that is done.

Guyver118Guyver118
that doesn't solve my problem, i want to show the error below a field like in standard pages that have validation rules.
SteveAnderson41SteveAnderson41

If I understand what you are asking, where you asked, "I am also having problems where i want my standard validation rules which are shown most at the field level, to be shown in my custom visualforce page?", and you are using a standard controller, it should just work without using apex:message.  From the developer guide,

 

If a user enters data on a Visualforce page that uses a standard controller, and that data causes a validation rule error, the error can be displayed on the Visualforcepage. If the validation rule error location is a field associated with an <apex:inputField> component, the error displays there. If the validation rule error location is set to the top of the page, use the <apex:pageMessages> or <apex:messages> component within the <apex:page> to display the error.

 

If you are using a custom controller, it's a bit harder.  You have to catch the error in your controller.  See http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_validation.htm

 

 

Message Edited by SteveAnderson41 on 01-28-2009 12:11 PM
Guyver118Guyver118
If i use a standard controller and an extension controller does this still hold true?
Message Edited by Guyver118 on 01-29-2009 02:12 AM
Ron HessRon Hess
Yes, your extension would try {} catch {} each DML operation and use addMessage() to place feedback on the page , in the location of the < apex : message > component
cbarry9cbarry9

For the new ApexPages.Message object that is passed to the addMessage() method, how would a person associate the message with a particular field back on the VF page? I assume this needs to be done to display the error message with the appropriate <apex:message> component. I see that there is a Message.getComponentLabel() method, but I do not see in the docs a Message.setComponentLabel() method or a way to pass that to the ApexPages.Message contructor.

 

thanks,

 

Chris

Volker_factory42Volker_factory42

I also have a problem to see apex:messages on Vf when using extensions and rendering action status:

 

VF:

 

<apex:page standardController="Object__c" extensions="CustumController" ><apex:messages />
<apex:pageBlock title="Data" >
<apex:pageBlockButtons >
<apex:commandButton value="Do something" action="{!DoSomething}" rerender="status" status="Status" />
</apex:pageBlockButtons>
<apex:actionStatus id="Status" startText="I'm doing something">
<apex:facet name="start">
<apex:outputtext > &nbsp;I'm doing something</apex:outputtext>
</apex:facet>
<apex:facet name="stop">
</apex:facet>
</apex:actionStatus>

 

 It is working when I'm not using rerender="Status" Status="Status". Any Idea how to get Apexmessages when using actionStatus?

 

Message Edited by Volker_SF42 on 06-26-2009 06:03 AM