tags:

views:

422

answers:

3

How to add a custom error message in sfValidator, the form is

$this->setWidgets(array(
 'name'    =>new sfWidgetFormInput(),
 'country' =>new sfWidgetFormChoice(array('choices' => CountriesPeer::getAllCountries())),
));

the validators

$this->setValidators(array(
 'name'    =>new sfValidatorString(array('required'=>true)),
 'country' =>new sfValidatorChoice(array('choices' =>     array_keys(CountriesPeer::getAllCountries()))),

  ));

instead of required, or invalid message i want a custom message(like 'name is required' 'please select a country'). I know that we can set custom error message while rendering the form, but can we set it in form validators??

A: 

the solution i got it from the symfony-froms book,

$this->setValidators(array(
'name'    =>new sfValidatorString(array('required'=>true),array('required' => 'The name   field is required.')),
'country' =>new sfValidatorChoice(array('choices' =>      array_keys(CountriesPeer::getAllCountries())),array('required' => 'please select a country')),

));
Harish Kurup
A: 

as Harish showed above that is the way to go, but there is also a Plugin http://www.symfony-project.org/plugins/sfViewableFormPlugin that will use yaml files to do application wide error messages, and combine that with the I18N mechanism you will have a nice way of showing errors.

Henrik Bjørnskov
+1  A: 

You can also use the setMessage method:

$this->validatorSchema['name']->setMessage('required', 'Name is required');
markb