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

veg*_*sta 2 php cakephp

在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')
            )
        )
    ));
?> 
Run Code Online (Sandbox Code Playgroud)

我想知道是否有一种方法来指定全局,所以我不需要每次创建调用都这样做.

sta*_*cke 6

在某处进行配置(例如: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')
        )
    )
));
Run Code Online (Sandbox Code Playgroud)

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

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

如果你需要更具体的某些特殊形式......

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