views:

35

answers:

2

Hi,

im trying to replace the default 'Required' message in this validator:

 $this->setValidator('email', new sfValidatorAnd(array(
   new sfValidatorEmail(array('required' => true, 'trim' => true)),
   new sfValidatorString(array('required' => true, 'max_length' => 80)),
   new sfValidatorDoctrineUnique(array(
     'model' => 'sfGuardUserProfile',
     'column' => 'email'
   ), array(
     'invalid' => 'An account with that email address already
exists. If you have forgotten your password, click "cancel", then "Reset
My Password."'))
 )));

but i don't know where should i add something like this: 'required' => 'You must write your e-mail').

Any idea?

Sf 1.4

Javi

A: 

Hello, you should add it in an array(), which should be second parameter of the sfValidatorEmail constructor.

 $this->setValidator('email', new sfValidatorAnd(array(
   new sfValidatorEmail(array('required' => true, 'trim' => true)),
   new sfValidatorString(array('required' => true, 'max_length' => 80)),
   new sfValidatorDoctrineUnique(array(
     'model' => 'sfGuardUserProfile',
     'column' => 'email'
   ), array(
     'invalid' => 'An account with that email address already
exists. If you have forgotten your password, click "cancel", then "Reset
My Password."'))
 ),array(), array('required' => 'test custom message')));
greg0ire
I tried what you say, but it's not working...$this->setValidator('email', new sfValidatorAnd(array( new sfValidatorEmail(array('required' => true, 'trim' => true), array('required' => 'This field is required')),
strange... why did you accept my answer then? Try removing the 'required' => true pari in sfValidatorString constructor, one 'required' must be sufficient.
greg0ire
People told me I should accept the answers, because my accept rate was low. For me is strange also to accept answers that are not usesful for me, but if i not accept them i would be in risk of nobody answer my questions..Anyway you proposal didn't work...
If you don't get any useful answer, offer a bounty, or delete your question... if you find a solution to your own question, answer it and accept your own answer.Regarding your problem, try adding the array in the sfValidatorAnd constructor, THIRD parameter this time. I posted some code to help you. This is because the required option defaults to true.
greg0ire
A: 

You have to define 'required' message for sfValidatorAnd:

$form->setValidator('email', new sfValidatorAnd(array(
   new sfValidatorEmail(array('required' => true, 'trim' => true)),
   new sfValidatorString(array('required' => true, 'max_length' => 80)),
   new sfValidatorDoctrineUnique(array(
     'model' => 'sfGuardUserProfile',
     'column' => 'email'
   ), array(
     'invalid' => 'An account with that email address already exists. If you have forgotten your password, click "cancel", then "Reset My Password."'))
 ), array(), array('required' => 'E-mail is required')));

sfValidatorAnd performs cleaning and it's responsible for setting error messages.

kuba