【问题标题】:cakePHP display Form elements inlinecakePHP 内联显示表单元素
【发布时间】:2012-05-15 10:11:38
【问题描述】:

我正在尝试显示使用 cakePHP 表单助手构建的内联表单元素。它似乎与它围绕元素创建的类有关。我确信这可以用 CSS 解决,但如果有另一种方法,我宁愿做对:

<?php
            echo $this->Form->create("users");
            echo $this->Form->input("username",array("label" => false, "class" => false, "value" => "Username", "class" => "loginCredentials"));
            echo $this->Form->input("password",array("label" => false, "class" => false, "value" => "Password", "class" => "loginCredentials"));
            echo $this->Form->input("loginBtn",array("label" => false, "class" => false, "value" => "LOGIN", "class" => "loginCredentials", "type" => "submit"));
            echo $this->Form->end();
            ?>

【问题讨论】:

  • 在你的 CSS 中搜索字符串 '.loginCredentials',它可能有一个 'display' 值或 'float' 应用于它,这会覆盖它们的默认显示:inline;。

标签: css cakephp helper


【解决方案1】:

在 CakePHP4 中,我只是简单地添加了一个新的 CSS 文件,在其他文件之后加载它,然后将以下内容放入其中。这会覆盖原始 CSS 中的现有 display:block

label,
legend {
    display: inline; //change
    font-size: 2.0rem;
    /* font-weight: 700; */
    /* margin-bottom: 2.0rem */
}

【讨论】:

    【解决方案2】:

    不要误会我的意思,我想你可以试试这个,这也应该有帮助

    echo $form->create('Job',
            array(  'type'=>'file',
                    'class' => 'form-horizontal',
                    'inputDefaults' => array(
                        'div' => array('class' => 'form-group'),
                        //needs to be redefined due to restrictions of the text element we need to define for each input..
                        'label' => array('class' => 'col-sm-4 col-md-3 control-label'),
                        'class' => 'form-control',
                        'between' => '<div class="col-sm-8 col-md-9">',
                        'after' => '</div>'
                    )
            )
        );
    

    那么你可以简单地做:

    echo $form->input('taskfield_id',
            array(  'label'=> array('text' => 'Aufgabenbereich&nbsp;*','class' => 'col-sm-4 col-md-3 control-label'),
                    'empty'=>'Bitte wählen'
            )
        );
    

    这会输出类似这样的东西

    <div class="form-group required">
        <label for="JobTaskfieldId" class="col-sm-4 col-md-3 control-label">Aufgabenbereich&nbsp;*</label>
        <div class="col-sm-8 col-md-9">
            <select name="data[Job][taskfield_id]" class="form-control" id="JobTaskfieldId">
                <option value="">Bitte wählen</option>
            </select>
        </div>
    </div>
    

    现在您有了一个可以始终为您提供这种输入形式的表单

    <div> <label> </label> <div> <input /> </div> </div>
    

    由于我需要翻译一些标签,因此我需要为每个输入创建一个 extea 标签数组,如果您将字段名称保持在数据库中,则不需要该数组。

    编辑: 这是用引导程序和蛋糕制成的, 您也可以自己设置样式,因为您可以完全控制类和标签 或不上课

    label{float:left;width:25%}
    label+div input{float:left;width:75%}
    

    【讨论】:

      【解决方案3】:

      我的表单只有 4 个必填字段,由以下视图生成:

      <div class="users form"> 
      <?php echo $form->create('User', array('class'=>'form'));?>
      <p class="formtitle">Sign Up</p>
      <fieldset>
          <legend class="formtitle">Please complete the form below.</legend>
          <?php
              echo $form->input('username',  array(
                  'label' => 'Login',
                  'div'=>'formfield',
                  'error' => array(
                      'wrap' => 'div',
                      'class' => 'formerror'
                      )
                  ));
              echo $form->input('password',  array(
                  'label' => 'Password',
                  'div'=>'formfield',
                  'error' => array(
                      'wrap' => 'div',
                      'class' => 'formerror'
                      )
                  ));
              echo $form->input('password_confirm', array(
                  'label' => 'Confirm password',
                  'type'=>'password',
                  'div'=>'formfield',
                  'error' => array(
                      'wrap' => 'div',
                      'class' => 'formerror'
                      )
                  ));
              echo $form->input('name',  array(
                  'label' => 'Name',
                  'div'=>'formfield',
                  'error' => array(
                      'wrap' => 'div',
                      'class' => 'formerror'
                      )
                  ));
              echo $form->input('surname',  array(
                  'label' => 'Surname',
                  'div'=>'formfield',
                  'error' => array(
                      'wrap' => 'div',
                      'class' => 'formerror'
                      )
                  ));
              echo $form->input('email',  array(
                  'label' => 'E-mail',
                  'div'=>'formfield',
                  'error' => array(
                      'wrap' => 'div',
                      'class' => 'formerror'
                      )
                  ));
              echo $form->input('city_id',  array(
                  'label' => 'City',
                  'div'=>'formfield',
                  'error' => array(
                      'wrap' => 'div',
                      'class' => 'formerror')));
          ?>
      </fieldset>  
      <?php echo $form->end('Submit');?>
      </div>
      

      一些解释:

      将类添加到表单标签:

      $form->create('User', array('class'=>'form'))
      generates:
      
      
      <form class="form" id="UserAddForm" method="post" action="/kultura/users/add">
      

      将类添加到输入:

      echo $form->input('username',  array(
                  'label' => 'Login',
                  'div'=>'formfield',
                  'error' => array(
                      'wrap' => 'div',
                      'class' => 'formerror'
                      )
                  ));
      

      生成这个:

      <div class="formfield required">
      <label for="UserUsername">Login</label>
      <input name="data[User][username]" type="text" maxlength="20" value="" id="UserUsername" />
      </div>
      

      这种情况下会出现错误:

      <div class=”formerror”></div>
      

      示例 css 样式可能如下所示:

      .form{
          font-family: Verdana;   
          font-size:1em;
          margin:1em;
          padding:1em;   
      }
      
      .form p.formtitle{
          color: #026475;
          font-size:1.3em;
          font-weight: bold;
      }
      
      .form fieldset{
          width:40em;
          border:1px solid #FFE545;  
          background-image: url(../img/Contact.png);
          background-position: bottom right;
          background-repeat: no-repeat;
      }
      
      .form fieldset legend{
          color: #026475;
      }
      
      .formfield{
          width:30em;
          padding:5px;
      }
      
      .formfield label{
          display:block;
          float:left;
          width:12em;
          padding:1px;
          color:#C2A34F;
          text-align:right;
      }
      
      .formfield input, .formfield  select{
          padding:0.15em;
          width:14em;
          border:1px solid #ddd;
          background:#FEFBAF;
      
          font-family: Verdana;   
          font-size:1em;
      
          -moz-border-radius:0.4em;
          -khtml-border-radius:0.4em;
      }
      
      .formfield input:hover, input:focus {
          border-color:#c5c5c5;
          background:#f6f6f6;
      }
      
      .required input {
          border:1px solid #FBB829;
      }
      
      .form .submit input{
          font-family: Verdana;   
          font-size:1em;
          margin-top: 0.3em;
      }
      
      .formerror{
          position:relative;
          left:12.1em;
          color:#FBB829;
      }
      

      结果:

      【讨论】:

        【解决方案4】:

        默认情况下,CakePHP 在输入周围放置&lt;div&gt;s。您可以将'div'=&gt;false 选项添加到每个输入:

        echo $this->Form->input("username",array("label" => false, "div"=>false, "class" => false, "value" => "Username", "class" => "loginCredentials"));
        

        或者,您可以为表单本身设置 inputDefaults:

        echo $this->Form->create('whatever', array(
            'inputDefaults'=>array('div'=>'false', 'label'=>false)));
        

        【讨论】:

        • 嘿戴夫,谢谢你的回答。我使用了 div => false 选项,但它不会删除提交按钮周围的 div,有没有办法删除 input[type=submit] 周围的 div ?
        • 我认为您的语法存在问题 - 它应该像任何其他输入一样在提交按钮上工作。
        • 要删除提交按钮周围的 div,您需要在进行 Form->end 调用时添加 div=> false 选项。所以把echo $this-Form-end();改成echo $this-&gt;Form-&gt;end(array('div' =&gt; false));
        • 这些都不适合我,我使用的是 CakePHP 3
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-08
        • 1970-01-01
        • 1970-01-01
        • 2012-08-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多