Input wrapper div class in CakePHP 3.0.0

橙三吉。 提交于 2019-12-01 15:57:46

问题


How can I change input wrapper div class in CakePHP 3.0.0.?

My code is:

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

and it returns:

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

I want output like:

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

回答1:


For CakePHP 3.0 versions ...

... there is no way to just pass on attributes to a template. You'd have to redefine the appropriate form helper templates.

You can either change them globally by using for example 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);

or only for a specific input via the templates option:

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
]);

See also

  • Cookbook > Views > Helpers > Form > Customizing the Templates FormHelper Uses

As of CakePHP 3.1 ...

... you can use so called template variables. You can placed them anywhere in a template

$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);

and use the templateVars option to define the values for them

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

See also

  • Cookbook > Views > Helpers > Form > Adding Additional Template Variables to Templates



回答2:


This code is working in my application. I think according to your requirement, this will useful.

<?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


来源:https://stackoverflow.com/questions/24140362/input-wrapper-div-class-in-cakephp-3-0-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!