cakePHP内联显示表单元素

x4shl7ld  于 2022-11-11  发布在  PHP
关注(0)|答案(4)|浏览(159)

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

mlnl4t2r1#

默认情况下,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)));
fae0ux8s

fae0ux8s2#

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

结果是:

tpxzln5u

tpxzln5u3#

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

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标签数组,用于每个输入,如果您保持字段名与数据库一致,则不需要该数组。
编辑:这是用bootstrap和cake制作的,你也可以自己设计风格,因为你可以完全控制类和标签,也可以不控制类

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

guicsvcw4#

在CakePHP4中,我只是添加了一个新的CSS文件,在其他文件之后加载它,并在其中放入以下内容。

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

相关问题