2014-09-26 31 views
6

所以我在工作中(爲了後代的緣故,我可能會加上敏感數據),以及我們需要使用Cakephp 3.0所有功能強大且最少記錄的新工具來決定的權力(測試版)。Cakephp 3多個自定義模板formhelpers

編輯:我的目標是創建幾個不同的模板,以通過formhelper模板或輸入方法調用窗體。這真的沒有什麼好的例子。 自定義表單助手採用模板: 正如本書上看到的(並且無處在互聯網上的任何地方)在很短的文件是這樣的: http://book.cakephp.org/3.0/en/core-libraries/helpers/form.html#customizing-the-templates-formhelper-uses

該網站說,你可以使用模板方法,然後給出一個模糊的「使用「:

$myTemplates = [ 
    'inputContainer' => '<div class="form-control">{{content}}</div>', 
]; 

$ this-> Form-> templates($ myTemplates);

然後它說你可以使用input()方法,它沒有給出任何例子。 最後但並非最不重要的是,自定義模板FormHelper應該允許你像許多這樣的自定義formhelpers「製作」或「創建」,但是他們沒有舉例說明如何做到這一點! lulwut?

我可以很容易地使用它,就像他們的例子一樣,但是單個自定義模板中的力量在哪裏呢?這對我毫無益處。

所以通過一個新的可能的解決方案,我嘗試並得到一個新的錯誤。

我得到這個錯誤(我的觀點中)(從下面的代碼):

致命錯誤 錯誤:類 '配置' 未找到

//within bootstrap.php 
Configure::write('templates', [ 
    'shortForm' => [ 
     'formstart' => '<form class="" {{attrs}}>', 
     'label' => '<label class="col-md-2 control-label" {{attrs}}>{{text}}</label>', 
     'input' => '<div class="col-md-4"><input type="{{type}}" name="{{name}}" {{attrs}} /></div>', 
     'select' => '<div class="col-md-4"><select name="{{name}}"{{attrs}}>{{content}}</select> </div>', 
     'inputContainer' => '<div class="form-group {{required}}" form-type="{{type}}">{{content}} </div>', 
     'checkContainer' => '',], 
    'longForm' => [ 
     'formstart' => '<form class="" {{attrs}}>', 
     'label' => '<label class="col-md-2 control-label" {{attrs}}>{{text}}</label>', 
     'input' => '<div class="col-md-6"><input type="{{type}}" name="{{name}}" {{attrs}} /></div>', 
     'select' => '<div class="col-md-6"><select name="{{name}}"{{attrs}}>{{content}}</select> </div>', 
     'inputContainer' => '<div class="form-group {{required}}" form-type="{{type}}">{{content}} </div>', 
     'checkContainer' => '',], 
    'fullForm' => [ 
     'formstart' => '<form class="" {{attrs}}>', 
     'label' => '<label class="col-md-2 control-label" {{attrs}}>{{text}}</label>', 
     'input' => '<div class="col-md-10"><input type="{{type}}" name="{{name}}" {{attrs}} /> </div>', 
     'select' => '<div class="col-md-10"><select name="{{name}}"{{attrs}}>{{content}}</select> </div>', 
     'inputContainer' => '<div class="form-group {{required}}" form-type="{{type}}">{{content}} </div>', 
     'checkContainer' => '',] 
]); 

//within my view 
<?php 
    $this->Form->templates(Configure::read('templates.shortForm')); 
?> 

舊更新:我添加

use "Cake\Core\Configure;" 

在我看來,一切都很好,但我想將它添加到層次結構中的適當文件y,這樣我就不必將它添加到每個視圖中,除非它當然會導致整個應用程序的功效問題,所以我不需要將它添加到每個視圖中,即

。有誰知道它應該進入哪個文件?問候和TIA!

最新更新:我只是想通了。很簡單!在下面檢查我的答案!希望這有助於有人

+0

那麼你想要什麼?這聽起來像是一個很長的投訴,但甚至沒有告訴我們你的目標是什麼?另外,如果仔細閱讀文檔,這不是關於單個模板,但您可以通過傳遞一組模板來使用templates()方法自定義任何輸出。 – burzum 2014-09-26 22:24:40

+0

正如我所說,是的,我同意這是文件說明,因爲它是顯而易見的。我的問題是...怎麼樣?你如何創建這些模板。他們甚至沒有在他們提供的例子中創建任何模板。他們只是以單一股票默認爲例。如果我想創建三個或四個單獨的自定義模板,這是如何完成的?我找到了一個網站:http://www.sanisoft.com/blog/2014/08/11/twitter-bootstrap-forms-cakephp-3/他做了類似的事情,但解釋說,由於缺乏文檔,他選擇通過FormHelper創建自定義小部件作爲自定義模板輸入表單。 – Urasquirrel 2014-09-26 22:44:52

回答

7

此修復程序的功能是允許您使用引導程序自定義模板表單(來自cakephp 3 !!!!)。如果你想使用表單助手設置大小以及它的所有優點(安全性和不安全性)。

CakePHP的何塞·扎普告訴我要嘗試引導插件和小工具,什麼不可以,但要做到這一點真正的出路應該是這樣的:

第1步:創建配置/ templatesConfig.php並添加自定義表單東東。

<?php 
$config = [ 
    'Templates'=>[ 
     'shortForm' => [ 
      'formStart' => '<form class="" {{attrs}}>', 
      'label' => '<label class="col-md-2 control-label" {{attrs}}>{{text}}</label>', 
      'input' => '<div class="col-md-4"><input type="{{type}}" name="{{name}}" {{attrs}} /></div>', 
      'select' => '<div class="col-md-4"><select name="{{name}}"{{attrs}}>{{content}}</select></div>', 
      'inputContainer' => '<div class="form-group {{required}}" form-type="{{type}}">{{content}}</div>', 
      'checkContainer' => '',], 
     'longForm' => [ 
      'formStart' => '<form class="" {{attrs}}>', 
      'label' => '<label class="col-md-2 control-label" {{attrs}}>{{text}}</label>', 
      'input' => '<div class="col-md-6"><input type="{{type}}" name="{{name}}" {{attrs}} /></div>', 
      'select' => '<div class="col-md-6"><select name="{{name}}"{{attrs}}>{{content}}</select></div>', 
      'inputContainer' => '<div class="form-group {{required}}" form-type="{{type}}">{{content}}</div>', 
      'checkContainer' => '',], 
     'fullForm' => [ 
      'formStart' => '<form class="" {{attrs}}>', 
      'label' => '<label class="col-md-2 control-label" {{attrs}}>{{text}}</label>', 
      'input' => '<div class="col-md-10"><input type="{{type}}" name="{{name}}" {{attrs}} /></div>', 
      'select' => '<div class="col-md-10"><select name="{{name}}"{{attrs}}>{{content}}</select></div>', 
      'inputContainer' => '<div class="form-group {{required}}" form-type="{{type}}">{{content}}</div>', 
      'checkContainer' => '',] 
    ] 
]; 

第2步:從您的控制器裏面的正確視圖的方法調用這一行。

不要忘記添加這對您的控制器頂部

use Cake\Core\Configure; 

$this->set('form_templates', Configure::read('Templates')); 

步驟3:將的bootstrap.php文件中添加此

// Load an environment local configuration file. 
// You can use this file to provide local overrides to your 
// shared configuration. 

Configure::load('templatesConfig','default'); //fixed 

第4步(最後):添加此行用你想要的模板名稱Bam !.

<?php $this->Form->templates($form_templates['shortForm']); ?> 
0

比方說,你需要的所有投入使用自定義標記的形式展現了硬編碼error-message錯誤輸入後標籤(默認爲前)和不同類:

$this->Form->create($entity, ['templates' => [ 
    'formGroup' => '{{input}}{{label}}', 
    'error' => '<div class="error">{{content}}</div>' 
]]); 

如果你只想定製單個輸入,通過相同的「模板」關鍵FormHelper::input()選項,如下所示:

$this->Form->input('fieldname', ['templates' => [ 
    'formGroup' => '{{input}}{{label}}', 
    'error' => '<div class="error">{{content}}</div>' 
]]); 

如果您需要定義多個模板,只要你想重新使用它們,這裏的你c的東西一試(介意我在這裏寫它,從來沒有使用過它):

// in bootstrap (as this is a config i believe 
Configure::write('templates', [ 
    'foo' => [....], 
    'bar' => [....] 
]); 

// in any view 
$this->Form->templates(Configure::read('templates.foo')); 
$this->Form->create(...); 
.... 

你也可以創建自己的助手,並定義模板那裏,等

這真的一切都取決於你想要什麼實現,但這應該讓你很好地理解模板是如何工作的(不僅僅是在形式上)。

+0

我試過你的最後一個例子,但它給出了一個奇怪的錯誤。就像我們都是,我是Cake 3.0 beta的新手。 「沒有找到類配置」混淆了我。 – Urasquirrel 2014-10-01 20:21:31

+0

更新:我添加了使用「Cake \ Core \ Configure;」在我看來,一切都很好,但我希望將其添加到層次結構中的適當文件中,這樣我就不必將其添加到每個視圖中,除非它會導致整個應用程序整體的功效問題。 – Urasquirrel 2014-10-01 20:29:57