You need to sign in to do that
Don't have an account?
HafaelDias
Displaying Validation error message in Visualforce pages
Hello!
I created a Visualforce page that have a Custom Object as a standardController. In that page I get one field from the Object and that field have a Validation rule. My question is:
How can I get the error message and display it in my own vf page?
'Cause when the Validation is activated, it shows a long and confusing (in the user point of view) message in the whole page. I'd like to show just a message in the Top of the page.
Thanks!
Life Is Too Short to Remove USB Safely
All Answers
You can display error or informational messages to the user on a Visualforce page by adding the <apex:pageMessages/> tag to your visualforce page. Usually you add this to the top of the page right under any commandbuttons that you have.
Then in your controller, you use the ApexPages.Message() and ApexPages.addMessage() methods to display the message like so:
Let me know if that helps.
Thanks for the replying!!!
Unfortunately it wasn't what I was looking for. Let me explain better:
When I have a validation rule in a field and I 'm using it in a custom page, the error message do not have the same behavior when I using in a standard Object page, like:
I create a field in a Custom Object and put a Validation rule on it. When I'm in the Object standard page, the error message appears next of the field or in the top. But, when I'm using my custom page, the page is redirected to another error page with the message:
Visualforce Error
System.DmlException: Update failed. First exception on row 0 with id xxxxxxxxxxxxxxxxx; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, "Error message: You can't do that!!": [Field]
I wanna get that error message and show in my page, without been redirected to that error page!
Btw, I didn't know this ApexPages.Message(). I'll use it in other occasion!! Thank for that!!!!
I don't think I have been in that situation before and have seen what you are seeing. You may be able to use the .hasMessage and .getMessage (page 340 in the Apex development guide) methods to determine if the validation message is firing and then do what you want with it.
The best alternative may be to get rid of the field validation that you created through the Salesforce UI and just validate the field in the controller's Apex code. Then you have more control of what happens in terms of the messaging to the users, what screen is shown to them, etc.
I'm trying don't validate in my controller.
I have some others validations to do and I'd like to let my code as simple as possible...
Thanks for the help MTBRider!
Do you have the <apex:pageMessages/> tag in your page?
Yes, I do!
But the problem isn't showing the message. It's to get the validation message error and then show it!
Sorry for all those days without posts but here I am!
Thank you davidjgriff, that was what I was looking for! That is my code:
Probably It isn't the best code but it works!
Thanks for all.