在 CakePHP 中,是否可以全局设置传递给 Form 助手创建方法的选项?
因为我想在我的所有表单上使用特定的表单布局,所以我目前在创建每个表单时都必须这样做。
<?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);
?>
关于php - 在 CakePHP 中,是否可以全局设置传递给 Form helper create 方法的选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9400247/