CakePHP 3.0.0中的输入包装器div类

Arc*_*ana 8 cakephp form-helpers cakephp-3.0

如何在CakePHP 3.0.0中更改输入包装器div类.

我的代码是:

<?= $this->Form->input('mobile',['div'=>['class'=>'col-md-4'],'class'=>'form-control','label'=>false]) ?>
Run Code Online (Sandbox Code Playgroud)

它返回:

<div class="input text">
    <input type="text" name="mobile" div="col-md-4" class="form-control" id="mobile">
</div>
Run Code Online (Sandbox Code Playgroud)

我想输出像:

<div class="col-md-4">
    <input type="text" name="mobile" class="form-control" id="mobile">
</div>
Run Code Online (Sandbox Code Playgroud)

ndm*_*ndm 18

对于CakePHP 3.0版本......

...没有办法将属性传递给模板.您必须重新定义相应的表单助手模板.

您可以使用以下方法全局更改它们FormHelper::templates():

$myTemplates = [
    'inputContainer' => '<div class="col-md-4 input {{type}}{{required}}">{{content}}</div>',
    'inputContainerError' => '<div class="col-md-4 input {{type}}{{required}} error">{{content}}{{error}}</div>'
];
$this->Form->templates($myTemplates);
Run Code Online (Sandbox Code Playgroud)

或仅通过templates选项进行特定输入:

echo $this->Form->input('mobile', [
    'templates' => [
        'inputContainer' => '<div class="col-md-4 input {{type}}{{required}}">{{content}}</div>',
        'inputContainerError' => '<div class="col-md-4 input {{type}}{{required}} error">{{content}}{{error}}</div>'
    ],
    'class' => 'form-control',
    'label' => false
]);
Run Code Online (Sandbox Code Playgroud)

也可以看看

截至CakePHP 3.1 ...

...你可以使用所谓的模板变量.您可以将它们放在模板中的任何位置

$myTemplates = [
    'inputContainer' => '<div class="input {{class}} {{type}}{{required}}">{{content}}</div>',
    'inputContainerError' => '<div class="input {{class}} {{type}}{{required}} error">{{content}}{{error}}</div>'
];
$this->Form->templates($myTemplates);
Run Code Online (Sandbox Code Playgroud)

并使用该templateVars选项为它们定义值

echo $this->Form->input('mobile', [
    'class' => 'form-control',
    'label' => false,
    'templateVars' => [
        'class' => 'col-md-4'
    ]
]);
Run Code Online (Sandbox Code Playgroud)

也可以看看

  • 这是3.0中的最终预期方式.通过方法选项修改生成的html是有限的,并且人们不断要求越来越多的选项使代码变得难以操作且难以维护.字符串模板提供了更大的灵活性,代码复杂性更低. (3认同)