php - 我如何更改 symfony 表单错误消息?

标签 php symfony1 symfony-1.4

我正在使用 symfony 1.4。

我有这样一个表格:

验证器:

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

View :

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

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

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


注意:这是一个显而易见的问题,但由于我花了很长时间才找到答案,所以我认为这个问题不值得在这里提出。

最佳答案

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表单的配置方法中添加

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

关于php - 我如何更改 symfony 表单错误消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7532612/

相关文章:

php - 使用 SWFUpload 和 PHP session 问题

php - javascript 函数在 document.ready() 下无法正常工作

javascript - 使用 php/javascript 在网站中创建在线 session 的视频 API 集成

mysql - 使用 Propel 的 SQL 查询(内连接 + 计数 + 分组依据)

mysql - 使用 doctrine 和 symfony 在数据库上执行函数

mysql - 问号DQL是不是没有考虑?

php - 修复 Innodb Integrity 约束违规 : 1452

symfony1 - 公开讨论 - Symfony - 你如何更快地开始你的项目?

symfony1 - sfDoctrineGuardPlugin 在数据转储后从 fixture 重新散列密码

Symfony 1 升级到 Symfony 2.8