2017-09-30 31 views
0

我試圖在我的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}}可能包含其他類。你有什麼想法如何做到這一點?

回答

1

解決方法: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', 
      ] 
     ); 
    } 
} 
+0

還有可用的插件,援助與創建引導兼容輸出:** HTTPS://github.com/FriendsOfCake/awesome-cakephp#templating**。 – ndm

+0

嗨,我知道,但插件僅與引導程序3一起工作。我使用bootstrap 4,它有很多變化。 – Matoran