【问题标题】:Input wrapper div class in CakePHP 3.0.0CakePHP 3.0.0 中的输入包装器 div 类
【发布时间】:2014-07-31 04:17:34
【问题描述】:

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

我的代码是:

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

然后它返回:

<div class="input text">
    <input type="text" name="mobile" div="col-md-4" class="form-control" id="mobile">
</div>

我想要这样的输出:

<div class="col-md-4">
    <input type="text" name="mobile" class="form-control" id="mobile">
</div>

【问题讨论】:

    标签: cakephp form-helpers cakephp-3.0


    【解决方案1】:

    对于 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);
    

    或仅用于通过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
    ]);
    

    另见

    截至 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);
    

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

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

    另见

    【讨论】:

    • 这是 3.0 中的最终预期方式。通过方法选项修改生成的 html 是有限制的,人们不断要求越来越多的选项,这使得代码变得笨拙且难以维护。字符串模板提供了更多的灵活性和更少的代码复杂性。
    • 有没有办法说明每个字段使用哪个模板?
    • 对于以后来这里的任何人 - 在 CakePHP 3.1 中添加了 templateVars - book.cakephp.org/3.0/en/views/helpers/…
    • @WarrenSergent-spakatak.com 我稍后会更新我的答案以包含它。
    • 谢谢@ndm。不幸的是,文档不是很清楚。
    【解决方案2】:

    此代码在我的应用程序中运行。我认为根据您的要求,这将很有用。

    <?php
                    echo $this->Form->input(
                    'name', [
                        'class' =>  'full-input',
                        'label' =>  'Class Name :',
                        'templates' => [
                            'inputContainer' => '<div class="full-input-wrapper">{{content}}</div>'
                        ]
                    ]);
                ?> 
    

    【讨论】:

      【解决方案3】:
      <div class="col-md-4">
       <?php echo $this->Form->input('mobile',['id' => 'mobile', 'class' => 'form-control']); ?>
      </div
      

      【讨论】:

      • 这会将类放在输入元素上,而不是在控件包装器周围。
      猜你喜欢
      • 2015-06-23
      • 2017-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-17
      • 2011-12-12
      相关资源
      最近更新 更多