【问题标题】:Cakephp 3 form input div in divCakephp 3表单在div中输入div
【发布时间】:2016-12-22 10:55:45
【问题描述】:

我最近使用 cakephp 3 开始了一个新项目。我必须为输入框生成一个特定的表单,因为我使用的是 AdminLTE V2。 管理员 LTE 所需的 HTML 如下

<div class="form-group">
    <label for="inputEmail3" class="col-sm-2 control-label">Email</label>

    <div class="col-sm-10">
        <input class="form-control" id="inputEmail3" placeholder="Email" type="email">
    </div>
</div>

我已经生成如下

<div class="form-group input text required">
    <label class="col-sm-3 control-label" for="full-name">Full Name</label>
    <input type="text" name="full_name" class="form-control" required="required" maxlength="100" id="full-name">
</div>

我需要生成内部&lt;div&gt;。我怎么能做到这一点?请帮忙。

【问题讨论】:

  • 你能贴出你在视图中使用的代码来生成表单吗?
  • 如果您确切知道自己需要什么,为什么每次都使用表单助手?直接使用该html。

标签: html cakephp input cakephp-3.0


【解决方案1】:

为此 CakePhp 提供自定义模板 FormHelper。与 CakePHP 中的许多助手一样,FormHelper 使用字符串模板来格式化它创建的 HTML。虽然默认模板旨在成为一组合理的默认值。您可能需要自定义模板以适合您的应用程序。

下面我已经根据 AdminLTE 自定义了输入字段。

echo $this->Form->input('email', [
'label' => ['text' => 'Email','class' => 'col-sm-2'],
'templates' => [
'label' => '<label{{attrs}}>{{text}}</label>',
'input' => '<div class="col-sm-10"><input type="{{type}}" name="{{name}}"  {{attrs}}/></div>',
'inputContainer' => '<div class="form-group {{type}}{{required}}">{{content}}</div>',
'inputContainerError' => '<div class="form-group  {{type}}{{required}} has-error">{{content}}{{error}}</div>',
],
]);

AdminLTE 主题的输出如下。

<div class="form-group text">
<label class="col-sm-2" for="email">Email</label>
   <div class="col-sm-10">
      <input type="text" name="email" placeholder="Email" id="first-name">
   </div>
</div>

【讨论】:

    【解决方案2】:

    使用 https://github.com/friendsofcake/bootstrap-ui 提供的蛋糕引导插件的朋友

    它已经完全支持基于引导程序的主题。

    【讨论】:

      【解决方案3】:

      正如@Manohar 所说,您可以只使用纯 html。但是如果你真的更喜欢 cakephp 的语法,我在我的项目中使用这种语法(相同的主题):

      <div class="form-group">
       <?php echo $this->Form->label('descricao', 'Descrição'); ?>
       <div class="col-sm-10">
        <?php echo $this->Form->input('descricao', ['label' => false, 'class' => 'form-control', 'placeholder' => "myPlaceHolder"]); ?>
       </div>
      </div>
      

      您可能需要一些额外的参数来完全按照纯 html 版本的代码编写的内容。您可以在其文档中查看 cakephp 表单的其他选项:http://book.cakephp.org/3.0/en/views/helpers/form.html#creating-form-inputs

      【讨论】:

        猜你喜欢
        • 2015-06-23
        • 2014-07-31
        • 2019-06-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多