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

标签 php cakephp

在 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/

相关文章:

php - cakePHP 类构造函数

php - CakePHP 语法从 $Auth 检索关联字段

php - 如何获取由cakephp中的updateAll()函数更新的行的id?

php - 尝试从 PHP 页面执行 node.js 命令

PHP - 编辑 zip 存档中的文件并在关闭前另存为另一个存档名称

php - 关于如何在 PHPUnit 中使用 setUp() 和 tearDown() 的真实示例?

php - Cakephp修改Join查询以添加计数而不是获取表的所有详细信息

php ffmpeg 获取视频时长

PHP Include 不使用 bootstrap 下拉菜单

php - 我如何在 CakePHP 中使用缓存?