【问题标题】:How do i change a symfony form error message?如何更改 symfony 表单错误信息?
【发布时间】:2011-09-23 17:19:17
【问题描述】:

我正在使用 symfony 1.4。

我有一个这样的表格:

验证者:

$this->setValidator('name', new sfValidatorString(array('required' => true)));    

查看:

<?php echo $form['name']->renderError() ?>

如何将默认的“必填”错误消息更改为,例如“此字段为必填项”?

编辑:另外,我如何摆脱renderError() 生成的&lt;ul&gt;&lt;li&gt; 标签。方法 ?我只想显示文本,没有额外的标记。


注意:这是一个显而易见的问题,但由于我花了很长时间才发现,我认为这个问题不适合在这里提出。

【问题讨论】:

    标签: php symfony1 symfony-1.4


    【解决方案1】:

    1.更改所需的消息:

    new sfValidatorString(
        array(
            'required' => true,
        ), 
        array(
            'required'=>'You custom required message'
        ));
    

    2.创建一个新的格式化程序类。

    3.重新定义你想要的属性。

    <?php
    class myWidgetFormSchemaFormatter extends sfWidgetFormSchemaFormatter
    {
    
      protected
        $rowFormat                 = '',
        $helpFormat                = '%help%',
        $errorRowFormat            = '%errors%',
        $errorListFormatInARow     = "  <ul class=\"error_list\">\n%errors%  </ul>\n",
        $errorRowFormatInARow      = "    <li>%error%</li>\n",
        $namedErrorRowFormatInARow = "    <li>%name%: %error%</li>\n",
        $decoratorFormat           = '',
        $widgetSchema              = null,
        $translationCatalogue      = null;
    

    4.在你的symfony表单的configure方法中添加

    $oDecorator = new myWidgetFormSchemaFormatter($this->getWidgetSchema());
    $this->getWidgetSchema()->addFormFormatter('myCustom', $oDecorator);
    $this->getWidgetSchema()->setFormFormatterName('myCustom');
    

    【讨论】:

      【解决方案2】:
      new sfValidatorString(array('required' => true), array('required'=>'This field is required'))
      

      您可以通过扩展 sfWidgetFormSchemaFormatter 来创建自定义的 WidgetFormSchemaFormatter 以格式化您的表单输出。

      【讨论】:

        猜你喜欢
        • 2020-02-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-10-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多