2015-12-21 78 views
2

嗯我寫了表單fields.But常見bootstrap css也適用於隱藏字段也如何限制隱藏字段在cakephp 3.x bootstrap。cakephp 3.0隱藏領域bootstrap問題

$myTemplates = [ 
     //'nestingLabel' => '<label{{attrs}}>{{text}}</label>{{input}}{{hidden}}', 
     'inputContainer' => '<div class="form-group order-status">{{content}}</div>', 
     'checkboxContainer' => '<div class="checkbox">{{content}}</div>', 
     'label' => '<label class="col-sm-2">{{text}}</label>', 
     'input' => '<div class="col-md-3"><input type="{{type}}" name="{{name}}" class="form-control" {{attrs}} /></div>', 
     'select' => '<div class="col-md-3"><select name="{{name}}"{{attrs}} class="form-control">{{content}}</select></div>',   
     'textarea'=> '<div class="col-md-8"><textarea name="{{name}}" {{attrs}} class="form-control"></textarea></div>',   
    ]; 
$this->Form->templates($myTemplates);?> 
<fieldset> 
    <legend><?php echo __('{0} Promotion', $edit ? 'Edit' : 'Add'); ?></legend> 
    <?php 
     if($edit) { 
      echo $this->Form->hidden('id'); 
      echo $this->Form->input('active', array('type' => 'checkbox','div'=>false)); 
     } else { 
      echo $this->Form->input('active', array('type' => 'checkbox','checked' => true)); 
     } 
     echo $this->Form->input('name'); 
     echo $this->Form->input('description'); 
     if($edit) { 
      echo $this->Form->input('promotion_type', array('type' => 'select', 'options' => Configure::read('Sidecart.ModelOptions.Promotion.promotion_types'), 'empty' => '-- Select One --', 'disabled' => true)); 
      echo $this->Form->hidden('promotion_type'); 
     } else { 
      echo $this->Form->input('promotion_type', array('type' => 'select', 'options' => Configure::read('Sidecart.ModelOptions.Promotion.promotion_types'), 'empty' => '-- Select One --')); 
     } 
      ?> 

回答

0

在CakePHP 3.0中,您可以使用Widgets來做到這一點。在一個視圖文件

<?php 
namespace App\View\Widget; 

use Cake\View\Widget\BasicWidget; 
use Cake\View\Form\ContextInterface; 
use Cake\View\Widget\WidgetInterface; 

class HiddenWidget extends BasicWidget implements WidgetInterface { 

    public function render(array $data, ContextInterface $context) { 
    $data += [ 
     'name' => '', 
    ]; 
    return $this->_templates->format('hidden' /* <-- Define the template name */, [ 
     'name' => $data['name'], 
     'attrs' => $this->_templates->formatAttributes($data, ['name']) 
    ]); 
    } 

} 

然後:

創建src\View\Widget\HiddenWidget.php

<?php 
$templates = [ // Define your templates 
    'hidden' => '<input type="hidden" name="{{name}}"{{attrs}}/>', 
    'input' => '<div class="col-md-3">' . 
       '<input type="{{type}}" name="{{name}}" class="form-control" {{attrs}}/>' . 
       '</div>' 
]; 

$this->Form->templates($templates); // Update the templates in FormHelper 
$this->Form->addWidget('hidden', ['Hidden']); // Add the HiddenWidget to FormHelper 

echo $this->Form->hidden('id'); // Renders with the 'hidden' template 
echo $this->Form->input('name'); // Renders with the 'input' template 

反之,你可能需要做相反的事情,創造BsFormControlWidget(S)來代替。這樣,輸入字段(隱藏的和可見的)保持其「即裝即用」功能。一般來說,您的自定義實施將與-Cake並肩而坐,似乎更傾向於建立在「頂部」而非變形。另外,您的小部件現在可以更容易地在其他項目中重用。