【问题标题】:Create icon inside of input with bootstrap and cakePHP使用 bootstrap 和 cakePHP 在输入内创建图标
【发布时间】:2015-07-05 16:14:39
【问题描述】:

我有这个 cakePHP 代码:

<?php

  echo $this->Form->input('about_me',
         array('label'  => __l('About Me'),
               'class'  => 'form-control',
               'before' => '<div class="form-group form-group-icon-left"><i class="fa fa-user input-icon input-icon-show"></i>',
               'after'  => '</div>',
               array('escape' => false)
         )
       );
?>

我希望输出为(为了便于阅读而缩进):

<div class="col-md-6">
 <div class="form-group form-group-icon-right"><i class="fa fa-map-marker input-icon"></i>
  <label>About me</label>
  <input class="form-control" placeholder="Write something" type="text" />
 </div>

有人可以帮忙吗?

【问题讨论】:

  • 你使用的是什么版本的 CakePHP?

标签: php html twitter-bootstrap cakephp input


【解决方案1】:

我的解决方案是这样的:

<div class="form-group form-group-icon-right">
    <i class="fa fa-map-marker input-icon"></i>
    <?= $this->Form->input('about_me', [
        'div' => false,
        'label' => 'About me',
        'placeholder' => 'Write something',
    ];?>
</div>

我通常设置'label' =&gt; false,只对输入字段使用CakePHP函数,并在纯HTML中自定义结构。

【讨论】:

    【解决方案2】:

    Skatch 的答案很好,但您需要进一步修改它以确保它考虑到验证。如果您不使用包装 div 的表单助手,则不会在适当的地方为您生成 errorrequired 类。

    您的示例代码看起来就差不多完成了。但是,不要在afterbefore 属性中设置&lt;div&gt;,而是使用div 属性来定义&lt;div&gt; 类:-

    <div class="col-md-6">
    <?php
      echo $this->Form->input('about_me',
         array('label'  => __l('About Me'),
               'class'  => 'form-control',
               'before' => '<i class="fa fa-user input-icon input-icon-show"></i> ',
               'div'  => 'form-group form-group-icon-left',
               'placeholder' => 'Write something'
            )
       );
    ?>
    </div>
    

    您还可以将placeholder 定义为输入的属性,如图所示。您不需要使用'escape' =&gt; false

    【讨论】:

    • @cashtrader 很高兴它成功了。请接受我的回答,以便其他搜索该网站的人可以快速找到您问题的解决方案。 :-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-09
    • 1970-01-01
    • 2014-05-19
    • 1970-01-01
    • 1970-01-01
    • 2020-02-18
    • 1970-01-01
    相关资源
    最近更新 更多