php Yii 2.0 如何在没有 <div class="form-group"> 的情况下生成表单?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27606834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:25:52  来源:igfitidea点击:

Yii 2.0 How to generate form without <div class="form-group">?

phpyiiyii2

提问by rakete

<?php $form = ActiveForm::begin(['id' => 'contact-form']); ?>
        <?= $form->field($model, 'email',  [
                'inputOptions' => [ 'placeholder' => 'Ihre E-Mail Adresse', 'class' => 'newsletter-cta-mail' ]
        ])->label(false)->textInput(); ?>
        <?= Html::submitButton('20 Gutschein sichern', ['class' => 'green newsletter-cta-button', 'name' => 'contact-button', 'value' => 'hallo']) ?>
<?php ActiveForm::end(); ?>

results into:

结果变成:

<form id="contact-form" action="/" method="post" role="form">
<input type="hidden" name="_csrf" value="WFlFWnIwU1Y3HnQKSn06GG46PXcjQRUzNCA9KhRiYCxvFXQ9RHIiPA==">     <div class="form-group field-newsletterform-email required has-error">

<input type="text" id="newsletterform-email" class="newsletter-cta-mail" name="NewsletterForm[email]" placeholder="Ihre E-Mail Adresse">

<p class="help-block help-block-error">Verification Code cannot be blank.</p>
</div>  <button type="submit" class="green newsletter-cta-button" name="contact-button" value="hallo">20 Gutschein sichern</button></form>

But I dont need the wrapping

但我不需要包装

How to disable this?

如何禁用此功能?

回答by soju

You could simply use Html::activeTextInput():

你可以简单地使用Html::activeTextInput()

<?= Html::activeTextInput($model, 'email', ['placeholder' => 'Ihre E-Mail Adresse', 'class' => 'newsletter-cta-mail']); ?>

Or change ActiveForm::$fieldConfigconfiguration :

或更改ActiveForm::$fieldConfig配置:

ActiveForm::begin([
    'id' => 'contact-form',
    'fieldConfig' => [
        'options' => [
            'tag' => false,
        ],
    ],
]); 

回答by Robert Fontanski

or You could something like this (change div to span)

或者你可以这样(将 div 更改为 span)



    $form = ActiveForm::begin([
    'id' => 'contact-form',
    'fieldConfig' => [
                        'template' => "{input}",
                        'options' => [
                            'tag'=>'span'
                        ]
    ]
    ]); 

回答by Oleg

<?= $form->field($model, 'email', [
    'template' => '{input}', // Leave only input (remove label, error and hint)
    'options' => [
        'tag' => false, // Don't wrap with "form-group" div
    ],
]) ?>

回答by Vikalp Veer

I have solved it like this....put tag as span.. also you can prepend icons infront of the box.

我已经解决了这个问题......把标签作为跨度......你也可以在框前面加上图标。

<div id="login-box-inner">
    <?php $form = ActiveForm::begin([
        'id' => 'login-form',
        'options' => ['role'=>'form'],

        'fieldConfig' => [
            'options' => [
                'tag' => 'span',
            ],

        ],
    ]); ?>

    <?= $form->field($model, 'username',[
        'template' => '
            <div class="input-group">
                <span class="input-group-addon"><i class="fa fa-user emerald"></i></span>
                {input}
            </div>
            {error}',
        'inputOptions' => [
            'placeholder' => 'Username ...',
            'class'=>'form-control',
        ]])
    ?>


    <?= $form->field($model, 'password', [
            'template' => '
                <div class="input-group">
                    <span class="input-group-addon"><i class="fa fa-key emerald"></i></span>
                    {input}
                </div>
                {error}',
            'inputOptions' => [
                'placeholder' => 'Password ...',
                'class'=>'form-control',
            ]])->input('password')
    ?>

    <?php ActiveForm::end(); ?>