cakephp 3.0隐藏归档引导问题

时间:2015-12-21 12:41:02

标签: php cakephp

嘿我为表单字段编写了常见的bootstrap css。但是也适用于隐藏字段。如何限制cakephp 3.x 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 --'));
        }
            ?>

1 个答案:

答案 0 :(得分:0)

在CakePHP 3.0中,您可以使用Widgets执行此操作。

创建src\View\Widget\HiddenWidget.php

<?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'])
    ]);
  }

}

然后在视图文件中:

<?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)。这样,输入字段(隐藏和可见)保持其开箱即用的#34;功能。你的自定义实现将与--Cake,通常,似乎更喜欢被构建&#34;在顶部&#34;而不是变形。此外,您的小部件现在可以更容易地在其他项目中重复使用。