开发者

To set custom error message in sfValidator?

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 c开发者_开发知识库an we set it in form validators??


You can also use the setMessage method:

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


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')),

));


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜