2009-11-26 50 views
1

如何sfValidator添加自定義錯誤消息,形式要在sfValidator中設置自定義錯誤消息?

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

的驗證

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

)); 

,而不是必需的,或者無效的消息,我想自定義消息(如「是必需的名字」 '請選擇一個國家')。 我知道我們可以在呈現表單時設置自定義錯誤消息,但是我們可以在表單驗證器中設置它嗎?

回答

0

我得到它從解決方案的symfony-320交織書,

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

)); 
2

您還可以使用setMessage方法:

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