-- Dividy <[EMAIL PROTECTED]> wrote
(on Tuesday, 05 February 2008, 04:25 AM -0800):
> Two questions now come to my mind :
>
> - Is it mandatory to use decorators to place a hint text below fields ?
Currently, yes, but I'm considering adding an additional decorator for
field and form hints for the 1.5.0 final release..
> - Is it possible to specify a different error message for the same error in
> two different fields ?
Yes -- by passing a messages option to the validator when creating the
element and/or validator.
$element->addValidator(
'NotEmpty',
false,
array('messages' => array('isEmpty' => 'Alternate message'))
);
You can also pull out the validator object and set the messages you
want.
$element->getValidator('NotEmpty')->setMessage('isEmpty', 'Alternate
message');
So, there are a variety of ways to do this. However, if you use a
translator object, then the translations will always be used, so the
same translation will be used in all instances of a particular validator.
> I mean this way (if it's not already possible, this code comes from ALkyD
> on Z-f.fr), he's got a sub-class Form which inherits of Zend_Form with a
> method setValidatorMessages(array $messages) :
>
> public function setValidatorMessages(array $messages)
> {
> $elements = $this->getElements();
> $i = 0;
>
> foreach ($elements as $el)
> {
> $validators = $el->getValidators();
>
> foreach ($validators as $v)
> {
> if (is_array($messages[$i]))
> $v->setMessages($messages[$i]);
> else
> $v->setMessage($messages[$i]);
>
> $i++;
> }
> }
> }
>
>
>
> And then in the form :
>
> // Construction du formulaire
> $login = new Zend_Form_Element_Text('txt_Login');
> $login->setLabel('Pseudo :')
> ->addValidator(new Zend_Validate_NotEmpty()); //
> Validateur n°1
>
> $pass = new Zend_Form_Element_Password('txt_Pass');
> $pass->setLabel('Mot de passe :')
> ->addValidator(new Zend_Validate_NotEmpty()) //
> Validateur n°2
> ->addValidator(new Zend_Validate_StringLength(4, 8)); //
> Validateur n°3
>
> $submit = new Zend_Form_Element_Submit('bt_Submit');
> $submit->setValue('Se connecter');
>
> $form = new Form();
> $form->setMethod('post')
> ->addElement($login)
> ->addElement($pass)
> ->addElement($submit);
> $form->setValidatorMessages(array(
> 'Le pseudo est obligatoire.', // Message pour le
> validateur n°1
> 'Le mot de passe est obligatoire.' // Message pour le validateur
> n°2
> array( // Messages pour le
> validateur n°3
> 'Le mot de passe est trop court',
> 'Le mot de passe est trop long'
> )
> ));
>
>
>
> Is that already possible or is it required to use this method ?
>
>
>
>
>
> Thanks! :-D
>
>
> Matthew Weier O'Phinney-3 wrote:
> >
> > -- Dividy <[EMAIL PROTECTED]> wrote
> > (on Monday, 04 February 2008, 09:42 AM -0800):
> >> Thanks for all your good advices. I tried everything you proposed some
> >> days
> >> ago and it didn't give any good results. That's why i have come with this
> >> solution.
> >>
> >> But please, if you could give me an example of line you'd put in a csv
> >> file,
> >> that would be great :)
> >>
> >> e.g. I tried using "isEmpty;The string is empty" in my en.csv file and
> >> then
> >> put the translation in my fr.csv file, but it didn't work :)
> >
> > You had it right... but there was a problem in trunk... while validation
> > in the *elements* was getting properly translated, the form object was
> > not injecting the translator into the elements... oops!
> >
> > Corrected now in svn trunk.
> >
> >
> >> Thanks in advance for your precious time.
> >
> > And thank you for the issue reporting!
> >
> >
> >
>
> --
> View this message in context:
> http://www.nabble.com/Zend_Form-and-validation-language-tp15248574s16154p15289026.html
> Sent from the Zend Framework mailing list archive at Nabble.com.
>
--
Matthew Weier O'Phinney
PHP Developer | [EMAIL PROTECTED]
Zend - The PHP Company | http://www.zend.com/