Zend Framework

Setting new error message don't work when a translated version have been set

Details

  • Type: Bug Bug
  • Status: Resolved Resolved
  • Priority: Major Major
  • Resolution: Won't Fix
  • Affects Version/s: 1.9.5
  • Fix Version/s: 1.10.0
  • Component/s: Zend_Validate
  • Labels:
    None

Description

If you specify a translated version of messages for errors in setDefaultTranslator($translator = null) (line 361), then it's impossible to overwrite it in execution's time.

The function _createMessage($messageKey, $value) (line 210) checks if there's a translated version of error message, but should also check if user has set an error's message during execution's time.

$validateStringLength = new Zend_Validate_StringLength();
$validateStringLength->setMin(1000)->setMessages(array(
    Zend_Validate_StringLength::TOO_SHORT => "Testing new error message"
));

The code above won't work if you have specified a translated version in Bootstrap.

All functions listed here are on file Zend_Validate_Abstract.

Activity

Hide
Thomas Weidner added a comment -

This can not work and will not be implemented.

Example:
You set an english error message.
You have activated translation/I18n.
Your user requests french translation.

But in your case the returned message is ALWAYS english as there would be no way to know which message has the same language as the requesting user and which has not and should be translated.

The solution:
When you activate translation then you must also set a translation for the validator which overrides the default/generic translation.
This will return the proper language, because when you don't set a new message, then the old default translation for this language will be returned.

Show
Thomas Weidner added a comment - This can not work and will not be implemented. Example: You set an english error message. You have activated translation/I18n. Your user requests french translation. But in your case the returned message is ALWAYS english as there would be no way to know which message has the same language as the requesting user and which has not and should be translated. The solution: When you activate translation then you must also set a translation for the validator which overrides the default/generic translation. This will return the proper language, because when you don't set a new message, then the old default translation for this language will be returned.
Hide
Thomas Weidner added a comment -

Closing as won't fix according to previous mentioned reasons.

Show
Thomas Weidner added a comment - Closing as won't fix according to previous mentioned reasons.
Hide
Rodrigo Novelo Primolan added a comment -

I used setDefaultTranslator() only to customize my errors' messages. Is there another way (a better method)?

Cya!

Show
Rodrigo Novelo Primolan added a comment - I used setDefaultTranslator() only to customize my errors' messages. Is there another way (a better method)? Cya!
Hide
Thomas Weidner added a comment -

But in your example you are using setMessage to customize your error messages additionally to translation.

As explained before: Eighter use one way or the other. Both ways can not work properly as there are always racing conditions.

Show
Thomas Weidner added a comment - But in your example you are using setMessage to customize your error messages additionally to translation. As explained before: Eighter use one way or the other. Both ways can not work properly as there are always racing conditions.
Hide
Rodrigo Novelo Primolan added a comment -

I used translation because I customized all error's messages.

How to customize all messages, without using translation's way or setMessage (one-by-one) ?

Ty

Show
Rodrigo Novelo Primolan added a comment - I used translation because I customized all error's messages. How to customize all messages, without using translation's way or setMessage (one-by-one) ? Ty
Hide
Erik Wijdemans added a comment -

I would like to comment on this one because it's in my opinion not possible to set a different translator for a validator when used in a form.

If the form is validated, the translator of the form will be passed through all elements and all validators of the elements.

Specifying a different translator will therefor be overridden and will only work if you set one for the form and will result in the translator being discarded for individual validators and form-elements.

In my opinion there should be a check to determine if a translator has been set for individual validators or formelements rather than just "blindly" passing the validator of the form onto it's children.

Show
Erik Wijdemans added a comment - I would like to comment on this one because it's in my opinion not possible to set a different translator for a validator when used in a form. If the form is validated, the translator of the form will be passed through all elements and all validators of the elements. Specifying a different translator will therefor be overridden and will only work if you set one for the form and will result in the translator being discarded for individual validators and form-elements. In my opinion there should be a check to determine if a translator has been set for individual validators or formelements rather than just "blindly" passing the validator of the form onto it's children.
Hide
Thomas Weidner added a comment -

This issue is not related to "Zend_Form" but to Zend_Validate itself.

When a form overrides the translator of Zend_Validate and you see this as false behaviour then the problem is within Zend_Form.

In Zend_Validate you can set a default translator for all validators and override it with a validator-specific translator.

When a form element set's it's own translator to the validators which are attached to the element it's on the form and not on the validator to check this.

Keep in mind that the validator itself has no way to determinate on which element he is connected. So the validator has no way to inject his own translator within the form element, or to get the translator which is set on the element.

Show
Thomas Weidner added a comment - This issue is not related to "Zend_Form" but to Zend_Validate itself. When a form overrides the translator of Zend_Validate and you see this as false behaviour then the problem is within Zend_Form. In Zend_Validate you can set a default translator for all validators and override it with a validator-specific translator. When a form element set's it's own translator to the validators which are attached to the element it's on the form and not on the validator to check this. Keep in mind that the validator itself has no way to determinate on which element he is connected. So the validator has no way to inject his own translator within the form element, or to get the translator which is set on the element.
Hide
Erik Wijdemans added a comment -

In this line

When a form element set's it's own translator to the validators which are attached to the element it's on the form and not on the validator to check this.

you say way i mean. In my opinion the form should check if an element already has a translator and the element should check if a validator already has a translator.

On a side-note referring to the original post, your solution regarding setting a translator for the validator will not work in combination with a form due to the mentioned reasons.

Show
Erik Wijdemans added a comment - In this line
When a form element set's it's own translator to the validators which are attached to the element it's on the form and not on the validator to check this.
you say way i mean. In my opinion the form should check if an element already has a translator and the element should check if a validator already has a translator. On a side-note referring to the original post, your solution regarding setting a translator for the validator will not work in combination with a form due to the mentioned reasons.
Hide
Rodrigo Novelo Primolan added a comment -

Please enlighten this to me:

The Translator has always priority over Zend_Validator (in this case, setting new error message through method setMessage)?

If so, shouldn't be different?

Thank you for yours patience

Show
Rodrigo Novelo Primolan added a comment - Please enlighten this to me: The Translator has always priority over Zend_Validator (in this case, setting new error message through method setMessage)? If so, shouldn't be different? Thank you for yours patience
Hide
Thomas Weidner added a comment -

@Erik:
As said before... the form must check if it wants to set a different translator... the validator can not check if the form which uses the validator has a translator set.

Even if I would agree to you (which I can not before looking into Zend_Form) this issue is still related to Zend_Validate and not Zend_Form.

When you have a problem with Zend_Form then please add a new issue to Zend_Form and not to Zend_Validate. Mixing different issues into a single one makes things just irritating for others.

@Rodrigo:
Setting a message manually you override all other things.
Think the other way round... when you set a own message, how should Zend_Translate translate this message? It does not know about it, so it can not be translated.

Show
Thomas Weidner added a comment - @Erik: As said before... the form must check if it wants to set a different translator... the validator can not check if the form which uses the validator has a translator set. Even if I would agree to you (which I can not before looking into Zend_Form) this issue is still related to Zend_Validate and not Zend_Form. When you have a problem with Zend_Form then please add a new issue to Zend_Form and not to Zend_Validate. Mixing different issues into a single one makes things just irritating for others. @Rodrigo: Setting a message manually you override all other things. Think the other way round... when you set a own message, how should Zend_Translate translate this message? It does not know about it, so it can not be translated.
Hide
Erik Wijdemans added a comment -

Excuse me for the misplaced comment about zend_form, I recreated it at http://framework.zend.com/issues/browse/ZF-9330

Show
Erik Wijdemans added a comment - Excuse me for the misplaced comment about zend_form, I recreated it at http://framework.zend.com/issues/browse/ZF-9330

People

Vote (0)
Watch (2)

Dates

  • Created:
    Updated:
    Resolved: