Php Symfony 1.4样式表单字段有错误
发生错误时,我想更改字段的背景色 在Java Struts中,我可以执行以下操作: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 {
<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');
}
}