我正在使用 CakePHP (2.x) 开发一个应用程序,并且每当我创建表单时我都会重用一些设置,所以我决定扩展默认的 FormHelper 类,如果没有其他任何东西传入它,它将自动加载默认值(见下面的代码)。
<?php
class AppFormHelper extends FormHelper
{
public function input($fieldName, $options = array())
{
$defaults = array(
'class' => 'input',
'div' => array(
'class' => 'button-height block-label margin-bottom'
)
);
$options = Set::merge($defaults, $options);
return parent::input($fieldName, $options);
}
}
乍一看,在我看来像这样调用它时,这似乎工作得很好$this->AppForm->input('test');
。但是,当提交该表单并出现错误时,该错误不会显示在屏幕上。调用时$this->Form->input('test')
出现错误,会创建一个如下所示的 div:
<div class="error-message">This form had an error</div>
最终,我只想有一个简单的方法来复制 FormHelper 的输入选项,并认为这是正确的方法,但由于它会产生问题,我不再确定。任何人都知道如何让错误再次出现,或者有更好的解决方案来为 FormHelper 提供默认选项。
谢谢!