I need to modify default error messages of doctrine validations. How
can I do this?
I need to modify default error messages of doctrine validations. How can I do
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
CrazyJoe is right, in a way : it is not possible without some hard work 🙁
But, if you search hard enough, you might find a way 😉
With Doctrine 1.1, you model classes extend
Doctrine_Record.That class defines this method :
This is the method that generates the messages ; as you can see, it’s fully automatic, and not configurable at all 🙁
Still, thanks to OOP, we can overload that method in our Model classes…
But, to be a bit cleaner, I would :
My_Doctrine_Record, that extendsDoctrine_RecordMy_Doctrine_Recordclass.This will avoid duplication of that method inside each of our model classes ; and might prove useful another day…
Our
My_Doctrine_Record::getErrorStackAsStringmethod can, of course, rely on a method of our model classes, to help generate the messages, with special customization for each model class.Here’s a working example ; far from perfect, but it might guide you to what you want to get 😉
First of all, the initialisations :
I’m guessing you already have something like that in your application…
Next, our new
My_Doctrine_Recordclass :You’ll notice that the
getErrorStackAsStringmethod is inspired by what is done by the one provided by Doctrine — this seems normal, I’d say ^^One other thing to be noticed :
getValidationFailedmethodnullif we want to use the default behabiour_getValidationFailedmethod in our Model classes, to customize stuff 😉And now, my Model class :
It extends
My_Doctrine_Record, and defines a_getValidationFailedmethod, that deals with validations errors on thenamefield of my model.Now, let’s suppose I do that to load a record :
Let’s try to modify it, setting up “bad” values :
Both
nameandvaluefields are not OK… So, we’ll go through our validations methods, and generate this error message :You can see the message for “
name” has been customized, and the one for “value” comes from the default Doctrine thing.So, to conclude : not easy, but do-able 😉
And, now, it’s up to you to use this as a guide to the exact solution to your problem 🙂
It will need some more coding, I think… But you’re not far away from the real deal !
Have fun !