【问题标题】:Removing <legend> and <fieldset> from FormHelper in CakePHP从 CakePHP 的 FormHelper 中删除 <legend> 和 <fieldset>
【发布时间】:2011-07-27 12:34:24
【问题描述】:

我正在对CakePHP 进行一些练习,我想从我从Form-&gt;create() 创建的表单中删除我通常避免使用的HTML 元素,例如&lt;legend&gt;&lt;fieldset&gt;。 我看到有这样的方式:

<?php
echo $this->Form->create('User', 
    array (
        'action' => 'login',
        'inputDefaults' => array (
            'fieldset' => false,
            'legend' => false
        )
    ));

    echo $this->Form->inputs(array (
        /*'legend' => __('Login', true),*/
        'username',
        'password'
    ));

    echo $this->Form->end('Login');
?>

如果我写这个,&lt;legend&gt;&lt;fieldset&gt; 将改为可见。 是否存在删除它们的特定选项?

<form accept-charset="utf-8" action="/site.com/users/login" method="post" id="UserLoginForm">
    <div style="display: none;"><input type="hidden" value="POST" name="_method"></div>
    <!-- hoto to remove this --><fieldset><legend>New User</legend><!-- end of removing -->
        <div class="input text required">
            <label for="UserUsername">Username</label><input type="text" id="UserUsername" maxlength="255" name="data[User][username]">
        </div>
        <div class="input password required"><label for="UserPassword">Password</label><input type="password" id="UserPassword" name="data[User][password]"></div>
    <!-- hoto to remove this --></fieldset><!-- end of removing -->
    <div class="submit"><input type="submit" value="Accedi"></div>
</form>

【问题讨论】:

    标签: forms cakephp option


    【解决方案1】:

    inputs() 函数是 1.3 的新功能,它允许您将字段捆绑在一个函数中并稍微整理一下。以下是杀死字段集和图例的方法:

    echo $this->Form->input(array(
        'legend' => false,
        'fieldset' => false,
        'username',
        'password'
    ));
    

    【讨论】:

    • +1 您必须同时使用两个选项 `'legend' => false, 'fieldset' => false,` 以去除字段集标签。
    【解决方案2】:

    问题是

    echo $this->Form->inputs(array (
        /*'legend' => __('Login', true),*/
        'username',
        'password'
    ));
    

    代替

    echo $this->Form->input('username');
    echo $this->Form->input('password');
    

    来自我在book.cakephp.org阅读的示例

    【讨论】:

      【解决方案3】:

      我从未见过表单助手生成&lt;fieldset&gt;&lt;legend&gt;。这将成为您观点的一部分。一个典型的视图文件(/app/views/items.ctp)

      <?php echo $this->Form->create('Item');?>
          <fieldset> <!-- remove this from your view (CTP) file -->
              <legend><?php __('Add Item'); ?></legend>
          <?php
              echo $this->Form->input('item_id');
              echo $this->Form->input('name');
          ?>
          </fieldset>
      <?php echo $this->Form->end(__('Submit', true));?>
      

      如您所见,&lt;fieldset&gt;&lt;legend&gt; 是视图中 HTML 的一部分。只需将其从标记中删除。 FormHelper 仅生成 FORM 的标记和一些实用程序输入。

      【讨论】:

      • 我没有在视图中写任何部分的html。该视图正是问题代码第一部分中的显示方式。
      • 你在使用脚手架吗?还要删除您拥有的所有选项数组(inputDefaults ...)只需使用干净的输入()。会发生什么?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-10
      • 1970-01-01
      • 1970-01-01
      • 2013-05-26
      • 2022-01-09
      • 2015-05-29
      相关资源
      最近更新 更多