2011-09-23 35 views
2

我正在使用symfony 1.4。我该如何更改symfony表单错误信息?

我有一个这样的形式:

验证:

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

观点:

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

如何更改 “所需的” 错误消息在默认情况下,例如,“此字段是必需的”?

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


注:这是一个明显的问题,但因为我花了很长时间找出来,我觉得这个问题diserves在这里问。

回答

5

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.In的symfony形式的配置方法添加

$oDecorator = new myWidgetFormSchemaFormatter($this->getWidgetSchema()); 
$this->getWidgetSchema()->addFormFormatter('myCustom', $oDecorator); 
$this->getWidgetSchema()->setFormFormatterName('myCustom'); 
4
new sfValidatorString(array('required' => true), array('required'=>'This field is required')) 

而且你可以通过扩展sfWidgetFormSchemaFormatter格式化您的形式输出创建一个自定义WidgetFormSchemaFormatter。