Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony 1.4样式表单字段有错误_Php_Validation_Symfony 1.4 - Fatal编程技术网

Php Symfony 1.4样式表单字段有错误

Php Symfony 1.4样式表单字段有错误,php,validation,symfony-1.4,Php,Validation,Symfony 1.4,发生错误时,我想更改字段的背景色 在Java Struts中,我可以执行以下操作: <s:textfield name="parameter" cssClass="normal_css_class" cssErrorClass="class_on_error" cssErrorStyle="style_on error"/> class sfWidgetFormSchemaFormatterCustom extends sfWidgetFormSchemaFormatter {

发生错误时,我想更改字段的背景色

在Java Struts中,我可以执行以下操作:

<s:textfield name="parameter" cssClass="normal_css_class" cssErrorClass="class_on_error" cssErrorStyle="style_on error"/>
class sfWidgetFormSchemaFormatterCustom extends sfWidgetFormSchemaFormatter
{
    protected
        $rowFormat                  = "<div class=\"%row_class%\">%label% %field% %hidden_fields% %help%</div>",
        $helpFormat                 = "%help%",
        $errorRowFormat             = "",
        $errorListFormatInARow      = "\n%errors%\n",
        $errorRowFormatInARow       = "<span class=\"error\">%error%</span>\n",
        $namedErrorRowFormatInARow  = "%error%\n",
        $decoratorFormat            = "%content%";


    public function formatRow($label, $field, $errors = array(), $help = '', $hiddenFields = null)
    {
        $row = parent::formatRow(
            $label,
            $field,
            $errors,
            $help,
            $hiddenFields
        );

        return strtr($row, array(
            '%row_class%' => (count($errors) > 0) ? ' error' : '',
        ));
    }
}// decorator class
class myForm extends sfForm
{
    public function configure()
    {
        // ....


        $formatter = new sfWidgetFormSchemaFormatterCustom($this->widgetSchema);
        $this->widgetSchema->addFormFormatter('custom', $formatter);
        $this->widgetSchema->setFormFormatterName('custom');
    }
}

我想能表演出类似的东西。当字段参数有错误时,标记呈现字段cssErrorClass。不再需要额外的Javascript

目前,我的模板中有以下(非常脏)代码:

<?php if($form['bill_to']->hasError()): ?>
  <?php echo $form['bill_to']->render(array('style' => 'background-color: red')) ?>
<?php else: ?>
  <?php echo $form['bill_to']->render() ?>
<?php endif; ?>
<?php echo $form['bill_to']->renderError() ?>

上面的代码可以工作,但是有没有一种方法可以实现它,所以我只需要调用:

<?php echo $form['bill_to']->render() ?>


然后它将执行样式的设置?我正在考虑重写render()方法,但我不确定这是否是正确的方法。

您可能需要查看表单格式化程序,请参阅


当您使用sfForm的configure方法时,可以使用
$this->getWidgetSchema()->GetFormFormFormatter()
获得FormFormFormatter对象。

您可以像这样扩展sfWidgetFormSchemaFormatter类:

<s:textfield name="parameter" cssClass="normal_css_class" cssErrorClass="class_on_error" cssErrorStyle="style_on error"/>
class sfWidgetFormSchemaFormatterCustom extends sfWidgetFormSchemaFormatter
{
    protected
        $rowFormat                  = "<div class=\"%row_class%\">%label% %field% %hidden_fields% %help%</div>",
        $helpFormat                 = "%help%",
        $errorRowFormat             = "",
        $errorListFormatInARow      = "\n%errors%\n",
        $errorRowFormatInARow       = "<span class=\"error\">%error%</span>\n",
        $namedErrorRowFormatInARow  = "%error%\n",
        $decoratorFormat            = "%content%";


    public function formatRow($label, $field, $errors = array(), $help = '', $hiddenFields = null)
    {
        $row = parent::formatRow(
            $label,
            $field,
            $errors,
            $help,
            $hiddenFields
        );

        return strtr($row, array(
            '%row_class%' => (count($errors) > 0) ? ' error' : '',
        ));
    }
}// decorator class
class myForm extends sfForm
{
    public function configure()
    {
        // ....


        $formatter = new sfWidgetFormSchemaFormatterCustom($this->widgetSchema);
        $this->widgetSchema->addFormFormatter('custom', $formatter);
        $this->widgetSchema->setFormFormatterName('custom');
    }
}