在 CakePHP 中,是否可以全局设置传递给表单助手创建方法的选项?

发布于 2025-01-08 02:42:31 字数 678 浏览 3 评论 0原文

在 CakePHP 中,是否可以全局设置传递给表单助手创建方法的选项?

由于我希望在所有表单上使用特定的表单布局,因此我当前在创建每个表单时都必须执行此操作。

<?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')
            )
        )
    ));
?> 

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

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

Since I want a specific form layout to be used on all my forms I am currently having to do this when I create every 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')
            )
        )
    ));
?> 

I was wondering if there was a way to specifiy this globally so I didn't need do it with every create call.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无妨# 2025-01-15 02:42:31

在某处进行配置(即: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);
?>

Make a configuration somewhere (ie: app/config/core.php -- or a similarly included file if you've expanded your configuration system)

// [...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')
        )
    )
));

Using it looks like this...

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

If you need to get more specific for certain special forms...

<?php
$more_options = array('class'=>'form-vertical');
$options = array_merge(Configure::read('MyGlobalFormOptions'), $more_options);
echo $this->Form->create('Profile', $options);
?>
謸气贵蔟 2025-01-15 02:42:31

starlocke的答案是好的,但我什至不想把这三行写得到处都是。 :) 我也不认为这真的是“配置数据”。所以这就是我要做的:

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

然后简单地调用它:

$this->MyForm->create('Profile');

或者在您想要更改某处的第二个参数中使用单个选项来调用它。

The answer of starlocke is ok but I would not even want to write these three lines all over the place. :) Neither I think this is really "config data". So here is what I would do:

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

Then simply call it:

$this->MyForm->create('Profile');

or call it with a single option in the 2nd param you want to change somewhere.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文