当使用CakePHP与Bootstrap时如何修改wrapper div错误类 [英] How to modify wrapper div error class when using CakePHP with Bootstrap

查看:103
本文介绍了当使用CakePHP与Bootstrap时如何修改wrapper div错误类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Bootstrap 3.0RC1 CakePHP 2.3.6 。尝试利用 has-error has-warning 验证状态,我需要更改默认元素类 FormHelper 添加到包装div。

I'm using Bootstrap 3.0RC1 with CakePHP 2.3.6. Trying to take advantage of those beautiful look classes like has-error and has-warning for validation states, I need to change the default element class FormHelper adds to the wrapping div.

到目前为止,我使用这个代码:

So far I'm using this code:

echo $this->Form->create('User', array(
    'inputDefaults' => array(
        'class' => 'form-control',
        'div' => array('class' => 'form-group'),
        'label' => array('class' => 'control-label'),
        'error' => array('attributes' => array('wrap' => 'span', 'class' => 'help-block'))
    )
)); 

echo $this->Form->input('email'));

错误时输出:

Which will output this on error:

<div class="form-group error">
    <label for="UserEmail" class="control-label">Email</label>
    <input name="data[User][email]" class="form-control form-error" type="email" value="a@a.com">
    <span class="help-block">Email already in use.</span>
</div>

一切都很好,除了我需要更改错误中的code>类包含 has-error ,因此将新样式应用于标签 输入 span 。到目前为止找不到一个干净的解决方案。

Everything is just fine, except that I need to change the error class in the wrapping div to has-error, so new styles are applied to the label, input and span. Couldn't find a clean solution so far.

我想的丑陋的解决方案是复制 has-error 从Bootstrap到我的应用程序中的错误类。

The ugly solution I thought is to copy has-error styles from Bootstrap to the error class in my app.

推荐答案

introspect FormHelper ,您将在这行中找到丑陋的代码,错误的魔法。

If you introspect FormHelper, you'll find in this line "ugly" code that do error magic.

由于原始作者没有任何机会通过配置这样做,我建议你写自己的 BootstrapFormHelper

Since original authors did not leave any chance to do this by configuration, I'm suggesting you writing own BootstrapFormHelper, and override input function, by changing that single line.

这里是代码片段:

//inside public function input($fieldName, $options = array())

$out['error'] = null;
if ($type !== 'hidden' && $error !== false) {
    $errMsg = $this->error($fieldName, $error);
    if ($errMsg) {
        $divOptions = $this->addClass($divOptions, 'has-error'); //old string value was 'error'
        if ($errorMessage) {
            $out['error'] = $errMsg;
        }
    }
}

custom BootstrapFormHelper 这里是完整的链接

只需将文件复制到 app\View\Helper ,然后添加到全部此行:

Just copy file to app\View\Helper, and add to ALL your Controllers this line:

public $helpers = array(
  'Form' => array('className' => 'BootstrapForm')
);



$ c>。

assuming that you've saved gist as BootstrapFormHelper.php.

这篇关于当使用CakePHP与Bootstrap时如何修改wrapper div错误类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆