cakePHP显示表格元素内联

时间:2012-05-06 18:19:01

标签: css cakephp helper

我正在尝试使用cakePHP Form帮助器显示内联构建的表单元素。它似乎与它围绕元素创建的类有关。我确信这可以用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();
            ?>

4 个答案:

答案 0 :(得分:5)

默认情况下,CakePHP将<div>放在输入周围。您可以为每个输入添加'div'=>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)));

答案 1 :(得分:0)

我的表单只有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;
}

结果:

enter image description here

答案 2 :(得分:0)

别误会我的意思,我想你可以尝试一下,这应该有所帮助

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>

因为我需要翻译一些标签,我需要为每个输入设置一个外部标签数组,如果你将字段名称保留为数据库,则不需要这样做。

编辑: 这是用bootstrap和蛋糕制成的, 你也可以自己设计风格,因为你可以完全控制类和标签 或没有班级

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

答案 3 :(得分:0)

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

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