在CakePHP中,可以全局设置传递给Form helper create方法的选项


In CakePHP is it possible to set the options you pass to the Form helper create method globally?

在CakePHP中,是否可以全局设置传递给Form helper create方法的选项?

由于我希望在我的所有表单上使用特定的表单布局,我目前在创建每个表单时都必须这样做。

<?php 
echo $this->Form->create('User', array(
    'class' => 'form-horizontal', 
    'inputDefaults' => array(
        'format' => array('before', 'label', 'between', 'input', 'error', 'after'), 
        'between' => '<div class="controls">', 
        'after' => '</div>', 
        'div' => 'control-group', 
        'error' => array(
            'attributes' => array('wrap' => 'span', 'class' => 'help-inline')
            )
        )
    ));
?> 

我想知道是否有一种方法可以在全局范围内指定这一点,这样我就不需要对每个创建调用都这样做了。

在某个地方进行配置(即:app/config/core.php——或者如果您扩展了配置系统,则为类似的包含文件)

// [...the rest of the config is above...]
Configure::write('MyGlobalFormOptions', array(
'class' => 'form-horizontal', 
'inputDefaults' => array(
    'format' => array('before', 'label', 'between', 'input', 'error', 'after'), 
    'between' => '<div class="controls">', 
    'after' => '</div>', 
    'div' => 'control-group', 
    'error' => array(
        'attributes' => array('wrap' => 'span', 'class' => 'help-inline')
        )
    )
));

使用它看起来像这样。。。

<?php
echo $this->Form->create('User', Configure::read('MyGlobalFormOptions'));
?>

如果你需要对某些特殊形式更具体。。。

<?php
$more_options = array('class'=>'form-vertical');
$options = array_merge(Configure::read('MyGlobalFormOptions'), $more_options);
echo $this->Form->create('Profile', $options);
?>

starlocke的答案是可以的,但我甚至不想把这三行写得到处都是。:)我也不认为这是真正的"配置数据"。所以我要做的是:

MyFormHelper extends FormHelper {
    public function create($model, $options) {
        $defaults = array(/* YOUR DEFAULT OPTIONS*/);
        $options = Set::merge($defaults, $options);
        //...
    }
}

然后简单地称之为:

$this->MyForm->create("配置文件");

或者在第二个参数中用一个选项调用它,你想在某个地方更改它。