【问题标题】:To set custom error message in sfValidator?在 sfValidator 中设置自定义错误消息?
【发布时间】:2009-11-26 05:53:26
【问题描述】:

如何在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()))),

  ));

我想要一条自定义消息而不是必需的或无效的消息(例如“需要姓名”“请选择一个国家/地区”)。 我知道我们可以在呈现表单时设置自定义错误消息,但是我们可以在表单验证器中设置它吗??

【问题讨论】:

    标签: php forms symfony1


    【解决方案1】:

    你也可以使用setMessage方法:

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

    【讨论】:

      【解决方案2】:

      我从 symfony-froms 书中得到的解决方案,

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

      【讨论】:

        【解决方案3】:

        正如 Harish 在上面展示的那样,这是要走的路,但还有一个插件 http://www.symfony-project.org/plugins/sfViewableFormPlugin 将使用 yaml 文件来处理应用程序范围的错误消息,并将其与 I18N 机制相结合,您将有一个很好的展示方式错误。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-02-12
          • 1970-01-01
          • 2023-04-03
          • 1970-01-01
          • 1970-01-01
          • 2022-08-23
          • 2011-08-15
          • 2013-09-23
          相关资源
          最近更新 更多