2

我正在尝试显示使用 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();
            ?>
4

4 回答 4

5

默认情况下,CakePHP<div>在输入周围放置 s。您可以将'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)));
于 2012-05-06T19:12:35.203 回答
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>

因为我需要翻译一些标签,所以每个输入都需要一个 extea 标签数组,如果您将字段名称保持在数据库中,则不需要这些标签。

编辑:这是用引导程序和蛋糕制作的,您也可以自己设计样式,因为您可以完全控制类和标签或没有类

label{float:left;width:25%}
label+div input{float:left;width:75%}
于 2014-11-18T15:54:20.590 回答
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”&gt;</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;
}

结果:

在此处输入图像描述

于 2013-12-31T05:52:22.710 回答
0

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

label,
legend {
    display: inline; //change
    font-size: 2.0rem;
    /* font-weight: 700; */
    /* margin-bottom: 2.0rem */
}
于 2021-07-15T09:26:44.380 回答