cakephp 3将默认类添加到输入 [英] cakephp 3 add default class to inputs

查看:62
本文介绍了cakephp 3将默认类添加到输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为cakephp 3应用程序中的每个输入添加默认类。
我想要的示例:

i'm trying to add a default class for each input in my cakephp 3 app. Example of what i want:

输入:
< echo $ this-> Form->控件('电子邮件');

输出:
< input class = form-control class = is-invalid />

所需的输出:
< input class = 表单控件无效 />

为此,我已经编辑了FormHelper的输入模板

for this i have edited input template of FormHelper

$this->viewBuilder()->setHelpers([
        'Form' => [
            'templates' => [
                'input' => '<input class="form-control" type="{{type}}" name="{{name}}"{{attrs}}/>'
            ]
        ]
    ]);

问题是 {{attrs}} 可能包含其他类。您有任何想法吗?

the problem is that {{attrs}} possibly contain others classes. Do you have any idea how to do it?

推荐答案

已解决:D
创建一个FormHelper来覆盖方法控件并添加类。

solved :D create a FormHelper to override method control and add class.

class BootstrapFormHelper extends FormHelper{
    public function control($fieldName, array $options = []){
        if($this->request->is('post') && !$this->isFieldError($fieldName)){
            $options['class'] = 'form-control is-valid';
        }else{
            $options['class'] = 'form-control';
        }
        return parent::control($fieldName, $options);
    }
}

然后更改您的AppView

then change your AppView

class AppView extends View{
    public function initialize()
    {
        $this->loadHelper(
            'Form', [
                'className' => 'BootstrapForm',
            ]
        );
    }
}

这篇关于cakephp 3将默认类添加到输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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