使用CakePHP和Bootstrap形成复选框和标签

使用CakePHP和Bootstrap形成复选框和标签,cakephp,twitter-bootstrap,Cakephp,Twitter Bootstrap,我在尝试使用CakePHP和Twitter引导获取复选框输入和标签以输出正确的HTML时遇到了一些问题 特定于引导的输出应为: <div class="control-group"> <div class="controls"> <label class="checkbox"> <input type="checkbox"> Keep me logged in </label> </div>

我在尝试使用CakePHP和Twitter引导获取复选框输入和标签以输出正确的HTML时遇到了一些问题

特定于引导的输出应为:

<div class="control-group">
  <div class="controls">
    <label class="checkbox">
      <input type="checkbox"> Keep me logged in
    </label>
  </div>
</div>
输出如下:

  <div class="control-group">
    <input type="hidden" name="data[User][auto_login]" id="UserAutoLogin_" value="0" />
    <input type="checkbox" name="data[User][auto_login]" class="" value="1" id="UserAutoLogin" />
    <div class="controls">
      <label for="UserAutoLogin">Keep me logged in</label>
    </div>
  </div>

让我登录

有没有办法调整这个单独的输入,使其输出正确的引导HTML,如上文所述

您需要为复选框手动构建表单小部件,而不是使用
FormHelper::input

例如:

echo '<div class="control-group">';
echo $this->Form->label('Model.field', null, array('class' => 'control-label')); 
echo '<div class="controls">';
echo $this->Form->checkbox('Model.field');
echo '</div>';
echo '</div>';
echo';
echo$this->Form->label('Model.field',null,array('class'=>'controllabel');
回声';
echo$this->Form->checkbox('Model.field');
回声';
回声';

如果您使用的是安全组件,如果您在没有
FormHelper::input的情况下创建输入,则可能会出现问题。如果是这种情况,您应该尝试以下方法:

echo "
<div class='control-group'>
    <div class='controls'>
        <label class='checkbox'>";                    
            echo $this->Form->input('Model.field', array('label' => false, 'after' => 'Model.field'))."
       </label>
    </div>
</div>";
'format' => array('before', 'label', 'input', 'between', 'after', 'error')
echo”
";                    
echo$this->Form->input('Model.field',array('label'=>false,'after'=>'Model.field'))。“
";

控制表单输出或输出格式的最佳方法是传递“format”参数。试着玩这个:

echo "
<div class='control-group'>
    <div class='controls'>
        <label class='checkbox'>";                    
            echo $this->Form->input('Model.field', array('label' => false, 'after' => 'Model.field'))."
       </label>
    </div>
</div>";
'format' => array('before', 'label', 'input', 'between', 'after', 'error')

我不认为它是文档,但通过阅读代码,您可以在其中找到它。

最漂亮的方法是:

<?php
echo '<div class="col-lg-2">';
echo $this->Form->input('Content.checkbox', array(
'div' => array(
    'class' => 'input-group',
),
'label' => false,
'type' => 'checkbox',
'before' => '<span class="input-group-addon">',
'after' => '</span><span class="input-group-addon"><label for="ContentCheckbox">What does the fox say?</label></span>',
));
echo '</div>';
尝试以下代码:

<div class="control-group">
    <div class="controls">
        <label class="checkbox">
            <?php echo $this->Form->input('auto_login', array('type'=>'checkbox','label' => false, 'div' => false,'class'=>false,'after'=>__('Keep me logged in')));?>
        </label>
    </div>
</div>

以下是现成的解决方案:

App::uses('FormHelper', 'View/Helper');
class InputHelper extends FormHelper {
    // var $helpers = array('Form', 'Html');
    public function create($model, $options = array()) {
        $options['class'] = (isset($options['class']) && $options['class']) ? $options['class'] : 'form-horizontal table';
        $options['inputDefaults'] = (isset($options['inputDefaults']) && $options['inputDefaults']) ? $options['inputDefaults'] : array(
            'div' => 'control-group',
            'label' => array('class' => 'control-label'),
            'between' => '<div class="controls">',
            'after' => '</div>'
        );
        return parent::create($model, $options);
    }

    public function input($fieldName, $options = array()) {
        $this->setEntity($fieldName);
        $options = $this->_parseOptions($options);
        if ($options['type'] == 'checkbox') {
            $options['format'] = array('before', 'label', 'between', 'input', 'after', 'error');
        }
        fdebug($options);
        return parent::input($fieldName, $options);
    }
}
App::使用('FormHelper','View/Helper');
类InputHelper扩展了FormHelper{
//var$helpers=array('Form','Html');
公共函数create($model,$options=array()){
$options['class']=(isset($options['class'])&&$options['class'])?$options['class']:'form horizontal table';
$options['inputDefaults']=(isset($options['inputDefaults'])&$options['inputDefaults'])?$options['inputDefaults']:数组(
'div'=>'控制组',
'label'=>array('class'=>controllabel'),
'在'=>''之间,
'在'=>''之后'
);
返回父项::创建($model$options);
}
公共函数输入($fieldName,$options=array()){
$this->setEntity($fieldName);
$options=$this->\u parseOptions($options);
如果($options['type']=='checkbox'){
$options['format']=数组('before','label','between','input','after','error');
}
fdebug(可选);
返回父::输入($fieldName,$options);
}
}

这里有一个简单的解决方案,对我来说很有用:

CSS(更少):

然后使用表单帮助器:

echo $this->Form->input(
    'field_name',
    array(
        'div' => 'form-group',
        'class' => 'form-control',
        'type' => 'checkbox',
        'label' => array(
            'text' => 'Your label',
            'class' => 'label-checkbox'
        ),
        'format' => array('input', 'label')
    )
);
如果你想要一个“一行”的答案,我知道了:

echo $this->Form->input('Model.Field', array('class'=>'form-inline', 'after'=>'</br>'));
echo$this->Form->input('Model.Field',数组('class'=>'Form-inline','after'=>'
');
I使用:

<?php 
echo $this->Form->input('coupDeCoeur', 
array('div' => false,
'label' => false,
'type' => 'checkbox',
'before' => '<label class="checkbox">',
'after' => '<i></i>coupDeCoeur</label>'
));
?>

在我的例子中,我将AdminLTE模板与Bootstrap 3一起使用,这段代码适用于我:

       <?php
            echo $this->Form->input('Model.fieldname', array(
                'label' => false,
                'type' => 'checkbox',
                'before' => '<label>',
                'after' => 'Field name label here</label>',
            ));
       ?>

祝你在这里好运