cakephp 3将默认类添加到输入
问题描述:
我试图在我的cakephp 3应用程序中为每个输入添加一个默认类。我想要什么 例子:cakephp 3将默认类添加到输入
输入: <echo $this->Form->control('email');
输出: <input class="form-control" class="is-invalid"/>
所需的输出: <input class="form-control is-invalid"/>
为了这个,我已编辑的表单助手
$this->viewBuilder()->setHelpers([
'Form' => [
'templates' => [
'input' => '<input class="form-control" type="{{type}}" name="{{name}}"{{attrs}}/>'
]
]
]);
输入模板
问题是{{attrs}}
可能包含其他类。你有什么想法如何做到这一点?
答
解决方法:D 创建FormHelper来覆盖方法控件并添加类。
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
class AppView extends View{
public function initialize()
{
$this->loadHelper(
'Form', [
'className' => 'BootstrapForm',
]
);
}
}
还有可用的插件,援助与创建引导兼容输出:** HTTPS://github.com/FriendsOfCake/awesome-cakephp#templating**。 – ndm
嗨,我知道,但插件仅与引导程序3一起工作。我使用bootstrap 4,它有很多变化。 – Matoran