|
Hi fellow hackers!
I'm writing an application using the Zend_Filter_Input class for filtering and validation. My app is multilingual and I have an instance of Zend_Translate up and running. The only problem is, that I'd like to translate the error messages, or better said the message templates, used by the Zend_Validate_* classes. Let's say I want to validate a value 'abc$123' with the Zend_Validate_Alnum validator. This would give me the message "'abc$123' has not only alphabetic and digit characters" Of course this is in english language, because that's how the message template is defined. Also at this point the placeholder %value% in the message template is already substituted by the value to check, so if I wanted to translate this message I'd first have to extract the value from the error message, translate the message and replace the value again, which leads to extreme regex (or str_replace for all you performance junkies ;-)) madness. Now what I'd like to do is, before using the validator, get all message templates, run them through the translator and use the translated templates instead. Something like foreach ($validator->_messageTemplates as $k => $v) { $validator->_messageTemplates[$k] = $translator->_($v); } Sadly the message templates are hideden in a protected property and there seems to be no get method or something, so there's no way to change them from the outside :-( The only ideas I came up with so far would be a) patch the ZF sources -> bad idea! b) Subclass each validator and add a get/setMessageTemplates method -> too complicated Any help on this topic would be greatly appreciated, cheers Sascha |
|
This may or may not be helpful, but I have a class in the Laboratory
(Zend_Validate_Builder_ErrorManager) that is designed to solve just such as use case: http://framework.zend.com/svn/laboratory/library/Zend/Validate/Builder/ErrorManager.php The only documentation I have, as of yet, is in this proposal page: http://framework.zend.com/wiki/x/fXM There are a also few unit tests in the laboratory for this class as well. If this seems like it might be useful, and you want more info, drop me a line. HTH, Bryce Lohr Sascha Göbel wrote: > Hi fellow hackers! > > I'm writing an application using the Zend_Filter_Input class for > filtering and validation. My app is multilingual and I have an instance > of Zend_Translate up and running. > > The only problem is, that I'd like to translate the error messages, or > better said the message templates, used by the Zend_Validate_* classes. > > Let's say I want to validate a value 'abc$123' with the > Zend_Validate_Alnum validator. This would give me the message > > "'abc$123' has not only alphabetic and digit characters" > > Of course this is in english language, because that's how the message > template is defined. > > Also at this point the placeholder %value% in the message template is > already substituted by the value to check, so if I wanted to translate > this message I'd first have to extract the value from the error message, > translate the message and replace the value again, which leads to > extreme regex (or str_replace for all you performance junkies ;-)) madness. > > Now what I'd like to do is, before using the validator, get all message > templates, run them through the translator and use the translated > templates instead. > > Something like > > foreach ($validator->_messageTemplates as $k => $v) { > $validator->_messageTemplates[$k] = $translator->_($v); > } > > Sadly the message templates are hideden in a protected property and > there seems to be no get method or something, so there's no way to > change them from the outside :-( > > The only ideas I came up with so far would be > > a) patch the ZF sources -> bad idea! > > b) Subclass each validator and add a get/setMessageTemplates method -> > too complicated > > Any help on this topic would be greatly appreciated, > cheers > Sascha > |
|
Hi Bryce,
this looks like a promising approach and feels more natural like the array config to me. I'll definitely have a look at this soon. Sadly it doesn't solve my problem with the message templates. Of course I could set the messages für each configuration separately, but the big picture I have in mind is to have a subclass of Zend_Filter_Input, check which validators are required and pass those validators message templates to the translator. The more I think of this, having a public method getMessageTemplates() in the Zend_Validate_Abstract class seems like the best approach to this. What do you think? What would be the recommended way to get such a change in the core? Thanks a lot in advance, Sascha Bryce Lohr schrieb: > This may or may not be helpful, but I have a class in the Laboratory > (Zend_Validate_Builder_ErrorManager) that is designed to solve just such > as use case: > http://framework.zend.com/svn/laboratory/library/Zend/Validate/Builder/ErrorManager.php > > > The only documentation I have, as of yet, is in this proposal page: > http://framework.zend.com/wiki/x/fXM > > There are a also few unit tests in the laboratory for this class as > well. If this seems like it might be useful, and you want more info, > drop me a line. > > HTH, > Bryce Lohr > > > Sascha Göbel wrote: >> Hi fellow hackers! >> >> I'm writing an application using the Zend_Filter_Input class for >> filtering and validation. My app is multilingual and I have an instance >> of Zend_Translate up and running. >> >> The only problem is, that I'd like to translate the error messages, or >> better said the message templates, used by the Zend_Validate_* classes. >> >> Let's say I want to validate a value 'abc$123' with the >> Zend_Validate_Alnum validator. This would give me the message >> >> "'abc$123' has not only alphabetic and digit characters" >> >> Of course this is in english language, because that's how the message >> template is defined. >> >> Also at this point the placeholder %value% in the message template is >> already substituted by the value to check, so if I wanted to translate >> this message I'd first have to extract the value from the error message, >> translate the message and replace the value again, which leads to >> extreme regex (or str_replace for all you performance junkies ;-)) >> madness. >> >> Now what I'd like to do is, before using the validator, get all message >> templates, run them through the translator and use the translated >> templates instead. >> >> Something like >> >> foreach ($validator->_messageTemplates as $k => $v) { >> $validator->_messageTemplates[$k] = $translator->_($v); >> } >> >> Sadly the message templates are hideden in a protected property and >> there seems to be no get method or something, so there's no way to >> change them from the outside :-( >> The only ideas I came up with so far would be >> >> a) patch the ZF sources -> bad idea! >> >> b) Subclass each validator and add a get/setMessageTemplates method -> >> too complicated >> >> Any help on this topic would be greatly appreciated, >> cheers >> Sascha >> > |
|
Hi Sascha,
There are several things I just don't like about how the Zend_Validate_* classes deal with error messages. If someone could shed some light on why things are like this, I'd be grateful: * getErrors() vs. getMessages(): Why not just have one method, getErrors(), that returns error messages key by error code? Why do the validator classes bother to keep these things in to separate, numerically-indexed arrays? It's just confusing... * protected _createMessage(): Why not make this method public? It puts the real values into the error message templates. This is a valuable service other classes could use. It would make my ErrorManager class a lot easier. It seems like it would be much easier to translate messages this way, too. I think that it would be better if, rather than having the validator classes manage their own error messages, they were managed from a central error manager class (which is what I was trying to create). Each time a validator finds an invalid value, it should call the error manager, perhaps as an observer, passing enough info to create an error message (perhaps including default message template). The error manager would aggregate the errors, and provide the client code with a nice API to query the error state. This error manager could then be easily replace at runtime, or integrated with Zend_Translate, and the client code has one easy-to-remember place to look for errors. BTW, there is a proposal template in the wiki. The "Submit a new proposal" link automatically creates an empty one from the template, I believe: http://framework.zend.com/wiki/display/ZFPROP/Home Regards, Bryce Lohr Sascha Goebel wrote: > Hi Bryce, > > sorry for the late answer, I'm pretty busy with my official work at the > moment, so there's not much room for spare time projects. :-( > > What additional changes do you have in mind? To be true, the only new > use case I can think of at the moment is the retrival of all message > templates or a direct integration of the Zend_Translate class. > > I'm pretty new to the whole Zend_Validate system, so I'm sure there are > a lot of pitfalls I'm just not aware of ;-) > > Is there any proposal template available, or are all proposals written > from scratch and the structure is just copied from other proposals? Also > I haven't signed a CLA yet and I think I have to talk to my employer > first before giving away all rights on my work ;-) But that's the second > step ... I think we should collect some ideas first. > > Cheers, > Sascha > > Bryce Lohr schrieb: > >> Hi Sascha, >> >> If you're looking for a way to globally change the error message >> templates for validators, then you're right, the ErrorManager class >> isn't so helpful for that. :) I had designed the API to set the message >> templates field-by-field, because the exact message is usually (in my >> apps) dependent on the field's specific position in the view. In other >> words, I don't often re-use validator message templates for different >> fields. >> >> I actually have several issues with the design of the error message >> handling in the Zend_Validate_* classes. The current design is simply >> makes working with error messages a lot harder than it should be. >> Personally, I think this aspect of the validators needs to be completely >> redesigned. >> >> To make this happen, however, a proposal needs to be created, and some >> of these use cases and specific issues need to be documented. I haven't >> actually collected all my thoughts on this, but if you want to get the >> ball rolling, I would willing to help. >> >> Regards, >> Bryce Lohr >> >> Sascha Goebel wrote: >> >>> Hi Bryce, >>> >>> this looks like a promising approach and feels more natural like the >>> array config to me. I'll definitely have a look at this soon. >>> >>> Sadly it doesn't solve my problem with the message templates. Of course >>> I could set the messages für each configuration separately, but the big >>> picture I have in mind is to have a subclass of Zend_Filter_Input, check >>> which validators are required and pass those validators message >>> templates to the translator. >>> >>> The more I think of this, having a public method getMessageTemplates() >>> in the Zend_Validate_Abstract class seems like the best approach to this. >>> >>> What do you think? >>> >>> What would be the recommended way to get such a change in the core? >>> >>> Thanks a lot in advance, >>> Sascha >>> >>> Bryce Lohr schrieb: >>> >>> >>>> This may or may not be helpful, but I have a class in the Laboratory >>>> (Zend_Validate_Builder_ErrorManager) that is designed to solve just such >>>> as use case: >>>> http://framework.zend.com/svn/laboratory/library/Zend/Validate/Builder/ErrorManager.php >>>> >>>> >>>> >>>> The only documentation I have, as of yet, is in this proposal page: >>>> http://framework.zend.com/wiki/x/fXM >>>> >>>> There are a also few unit tests in the laboratory for this class as >>>> well. If this seems like it might be useful, and you want more info, >>>> drop me a line. >>>> >>>> HTH, >>>> Bryce Lohr >>>> >>>> >>>> Sascha Göbel wrote: >>>> >>>> >>>>> Hi fellow hackers! >>>>> >>>>> I'm writing an application using the Zend_Filter_Input class for >>>>> filtering and validation. My app is multilingual and I have an instance >>>>> of Zend_Translate up and running. >>>>> >>>>> The only problem is, that I'd like to translate the error messages, or >>>>> better said the message templates, used by the Zend_Validate_* classes. >>>>> >>>>> Let's say I want to validate a value 'abc$123' with the >>>>> Zend_Validate_Alnum validator. This would give me the message >>>>> >>>>> "'abc$123' has not only alphabetic and digit characters" >>>>> >>>>> Of course this is in english language, because that's how the message >>>>> template is defined. >>>>> >>>>> Also at this point the placeholder %value% in the message template is >>>>> already substituted by the value to check, so if I wanted to translate >>>>> this message I'd first have to extract the value from the error >>>>> message, >>>>> translate the message and replace the value again, which leads to >>>>> extreme regex (or str_replace for all you performance junkies ;-)) >>>>> madness. >>>>> >>>>> Now what I'd like to do is, before using the validator, get all message >>>>> templates, run them through the translator and use the translated >>>>> templates instead. >>>>> >>>>> Something like >>>>> >>>>> foreach ($validator->_messageTemplates as $k => $v) { >>>>> $validator->_messageTemplates[$k] = $translator->_($v); >>>>> } >>>>> >>>>> Sadly the message templates are hideden in a protected property and >>>>> there seems to be no get method or something, so there's no way to >>>>> change them from the outside :-( >>>>> The only ideas I came up with so far would be >>>>> >>>>> a) patch the ZF sources -> bad idea! >>>>> >>>>> b) Subclass each validator and add a get/setMessageTemplates method -> >>>>> too complicated >>>>> >>>>> Any help on this topic would be greatly appreciated, >>>>> cheers >>>>> Sascha >>>>> >>>>> |
|
Hi Bryce,
when reading the looooong Zend_Form thread, I think Mathew already talked about modifying the Zend_Validate_* classes for easier access to the message templates. Maybe we should check the outcome of this project before proposing the complete rework of the classes? What do you think? I think there was also a reference to the ErrorManager and the Validate Builder, so this sounds like a pretty complete solution. As for the proposal process, I think I simply don't see the contents because I'm not logged in and haven't signed the CLA yet. Best regards, Sascha Bryce Lohr schrieb: > Hi Sascha, > > There are several things I just don't like about how the Zend_Validate_* > classes deal with error messages. If someone could shed some light on > why things are like this, I'd be grateful: > > * getErrors() vs. getMessages(): Why not just have one method, > getErrors(), that returns error messages key by error code? Why do the > validator classes bother to keep these things in to separate, > numerically-indexed arrays? It's just confusing... > * protected _createMessage(): Why not make this method public? It puts > the real values into the error message templates. This is a valuable > service other classes could use. It would make my ErrorManager class a > lot easier. It seems like it would be much easier to translate messages > this way, too. > > I think that it would be better if, rather than having the validator > classes manage their own error messages, they were managed from a > central error manager class (which is what I was trying to create). Each > time a validator finds an invalid value, it should call the error > manager, perhaps as an observer, passing enough info to create an error > message (perhaps including default message template). The error manager > would aggregate the errors, and provide the client code with a nice API > to query the error state. This error manager could then be easily > replace at runtime, or integrated with Zend_Translate, and the client > code has one easy-to-remember place to look for errors. > > BTW, there is a proposal template in the wiki. The "Submit a new > proposal" link automatically creates an empty one from the template, I > believe: > http://framework.zend.com/wiki/display/ZFPROP/Home > > Regards, > Bryce Lohr > > > Sascha Goebel wrote: >> Hi Bryce, >> >> sorry for the late answer, I'm pretty busy with my official work at the >> moment, so there's not much room for spare time projects. :-( >> >> What additional changes do you have in mind? To be true, the only new >> use case I can think of at the moment is the retrival of all message >> templates or a direct integration of the Zend_Translate class. >> >> I'm pretty new to the whole Zend_Validate system, so I'm sure there are >> a lot of pitfalls I'm just not aware of ;-) >> >> Is there any proposal template available, or are all proposals written >> from scratch and the structure is just copied from other proposals? Also >> I haven't signed a CLA yet and I think I have to talk to my employer >> first before giving away all rights on my work ;-) But that's the second >> step ... I think we should collect some ideas first. >> >> Cheers, >> Sascha >> >> Bryce Lohr schrieb: >> >>> Hi Sascha, >>> >>> If you're looking for a way to globally change the error message >>> templates for validators, then you're right, the ErrorManager class >>> isn't so helpful for that. :) I had designed the API to set the message >>> templates field-by-field, because the exact message is usually (in my >>> apps) dependent on the field's specific position in the view. In other >>> words, I don't often re-use validator message templates for different >>> fields. >>> >>> I actually have several issues with the design of the error message >>> handling in the Zend_Validate_* classes. The current design is simply >>> makes working with error messages a lot harder than it should be. >>> Personally, I think this aspect of the validators needs to be completely >>> redesigned. >>> >>> To make this happen, however, a proposal needs to be created, and some >>> of these use cases and specific issues need to be documented. I haven't >>> actually collected all my thoughts on this, but if you want to get the >>> ball rolling, I would willing to help. >>> >>> Regards, >>> Bryce Lohr >>> >>> Sascha Goebel wrote: >>> >>>> Hi Bryce, >>>> >>>> this looks like a promising approach and feels more natural like the >>>> array config to me. I'll definitely have a look at this soon. >>>> >>>> Sadly it doesn't solve my problem with the message templates. Of course >>>> I could set the messages für each configuration separately, but the big >>>> picture I have in mind is to have a subclass of Zend_Filter_Input, >>>> check >>>> which validators are required and pass those validators message >>>> templates to the translator. >>>> >>>> The more I think of this, having a public method getMessageTemplates() >>>> in the Zend_Validate_Abstract class seems like the best approach to >>>> this. >>>> >>>> What do you think? >>>> >>>> What would be the recommended way to get such a change in the core? >>>> >>>> Thanks a lot in advance, >>>> Sascha >>>> >>>> Bryce Lohr schrieb: >>>> >>>> >>>>> This may or may not be helpful, but I have a class in the Laboratory >>>>> (Zend_Validate_Builder_ErrorManager) that is designed to solve just >>>>> such >>>>> as use case: >>>>> http://framework.zend.com/svn/laboratory/library/Zend/Validate/Builder/ErrorManager.php >>>>> >>>>> >>>>> >>>>> >>>>> The only documentation I have, as of yet, is in this proposal page: >>>>> http://framework.zend.com/wiki/x/fXM >>>>> >>>>> There are a also few unit tests in the laboratory for this class as >>>>> well. If this seems like it might be useful, and you want more info, >>>>> drop me a line. >>>>> >>>>> HTH, >>>>> Bryce Lohr >>>>> >>>>> >>>>> Sascha Göbel wrote: >>>>> >>>>>> Hi fellow hackers! >>>>>> >>>>>> I'm writing an application using the Zend_Filter_Input class for >>>>>> filtering and validation. My app is multilingual and I have an >>>>>> instance >>>>>> of Zend_Translate up and running. >>>>>> >>>>>> The only problem is, that I'd like to translate the error >>>>>> messages, or >>>>>> better said the message templates, used by the Zend_Validate_* >>>>>> classes. >>>>>> >>>>>> Let's say I want to validate a value 'abc$123' with the >>>>>> Zend_Validate_Alnum validator. This would give me the message >>>>>> >>>>>> "'abc$123' has not only alphabetic and digit characters" >>>>>> >>>>>> Of course this is in english language, because that's how the message >>>>>> template is defined. >>>>>> >>>>>> Also at this point the placeholder %value% in the message template is >>>>>> already substituted by the value to check, so if I wanted to >>>>>> translate >>>>>> this message I'd first have to extract the value from the error >>>>>> message, >>>>>> translate the message and replace the value again, which leads to >>>>>> extreme regex (or str_replace for all you performance junkies ;-)) >>>>>> madness. >>>>>> >>>>>> Now what I'd like to do is, before using the validator, get all >>>>>> message >>>>>> templates, run them through the translator and use the translated >>>>>> templates instead. >>>>>> >>>>>> Something like >>>>>> >>>>>> foreach ($validator->_messageTemplates as $k => $v) { >>>>>> $validator->_messageTemplates[$k] = $translator->_($v); >>>>>> } >>>>>> >>>>>> Sadly the message templates are hideden in a protected property and >>>>>> there seems to be no get method or something, so there's no way to >>>>>> change them from the outside :-( >>>>>> The only ideas I came up with so far would be >>>>>> >>>>>> a) patch the ZF sources -> bad idea! >>>>>> >>>>>> b) Subclass each validator and add a get/setMessageTemplates >>>>>> method -> >>>>>> too complicated >>>>>> >>>>>> Any help on this topic would be greatly appreciated, >>>>>> cheers >>>>>> Sascha >>>>>> > |
|
Sascha Goebel wrote:
> Hi Bryce, > > when reading the looooong Zend_Form thread, I think Mathew already > talked about modifying the Zend_Validate_* classes for easier access > to the message templates. > > Maybe we should check the outcome of this project before proposing the > complete rework of the classes? What do you think? > I agree. I think what comes out of Matthew's work will be a big step forward. Hopefully, through Zend_Form, he will get the necessary changes made to make central error handling possible. Regards, Bryce Lohr |
|
Hi Bryce and Sascha,
Matthew and I have discussed some changes to the validation classes, and when I take a look at them, I'll also keep in mind how better we can support your use cases. I think we'll need this functionality, too, for Zend_Form, and I'm confident we'll arrive upon a reasonable solution. :) Best regards, Darby Bryce Lohr wrote: > Sascha Goebel wrote: >> Hi Bryce, >> >> when reading the looooong Zend_Form thread, I think Mathew already >> talked about modifying the Zend_Validate_* classes for easier access >> to the message templates. >> >> Maybe we should check the outcome of this project before proposing the >> complete rework of the classes? What do you think? >> > I agree. I think what comes out of Matthew's work will be a big step > forward. Hopefully, through Zend_Form, he will get the necessary changes > made to make central error handling possible. > > Regards, > Bryce Lohr > |
|
Thanks Darby! That's encouraging news. I'll keep my eyes open. :)
Regards, Bryce Lohr Darby Felton wrote: > Hi Bryce and Sascha, > > Matthew and I have discussed some changes to the validation classes, and > when I take a look at them, I'll also keep in mind how better we can > support your use cases. I think we'll need this functionality, too, for > Zend_Form, and I'm confident we'll arrive upon a reasonable solution. :) > > Best regards, > Darby > > Bryce Lohr wrote: > >> Sascha Goebel wrote: >> >>> Hi Bryce, >>> >>> when reading the looooong Zend_Form thread, I think Mathew already >>> talked about modifying the Zend_Validate_* classes for easier access >>> to the message templates. >>> >>> Maybe we should check the outcome of this project before proposing the >>> complete rework of the classes? What do you think? >>> >>> >> I agree. I think what comes out of Matthew's work will be a big step >> forward. Hopefully, through Zend_Form, he will get the necessary changes >> made to make central error handling possible. >> >> Regards, >> Bryce Lohr >> >> |
|
Hi,
yes it's great to hear this. I'll try to keep track of the developments regarding form and error handling and dig deeper into this topic during the holidays :-) Keep up the good work Sascha Bryce Lohr schrieb: > Thanks Darby! That's encouraging news. I'll keep my eyes open. :) > > Regards, > Bryce Lohr > > > Darby Felton wrote: >> Hi Bryce and Sascha, >> >> Matthew and I have discussed some changes to the validation classes, and >> when I take a look at them, I'll also keep in mind how better we can >> support your use cases. I think we'll need this functionality, too, for >> Zend_Form, and I'm confident we'll arrive upon a reasonable solution. :) >> >> Best regards, >> Darby >> >> Bryce Lohr wrote: >> >>> Sascha Goebel wrote: >>> >>>> Hi Bryce, >>>> >>>> when reading the looooong Zend_Form thread, I think Mathew already >>>> talked about modifying the Zend_Validate_* classes for easier access >>>> to the message templates. >>>> >>>> Maybe we should check the outcome of this project before proposing the >>>> complete rework of the classes? What do you think? >>>> >>>> >>> I agree. I think what comes out of Matthew's work will be a big step >>> forward. Hopefully, through Zend_Form, he will get the necessary changes >>> made to make central error handling possible. >>> >>> Regards, >>> Bryce Lohr >>> >>> > |
| Powered by Nabble | Edit this page |
