Cakephp 3在div中形成输入div

时间:2016-12-22 10:55:46

标签: html cakephp input cakephp-3.0

我最近使用cakephp 3开始一个新项目。我必须为输入框生成一个特定的表单,因为我使用的是AdminLTE V2。 Admin 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>

我需要生成内部<div>。我怎么能这样做?请帮忙。

3 个答案:

答案 0 :(得分:3)

为此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>

答案 1 :(得分:0)

使用https://github.com/friendsofcake/bootstrap-ui

提供的cake bootstrap插件的朋友

它已经包含对基于bootstrap的主题的完全支持。

答案 2 :(得分:-1)

你可以使用普通的html,正如@Manohar所说。但是如果你真的更喜欢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