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

rak*_*ete 24 php yii yii2

<?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(); ?>
Run Code Online (Sandbox Code Playgroud)

结果成:

<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>
Run Code Online (Sandbox Code Playgroud)

但我不需要包装

如何禁用此功能?

soj*_*oju 32

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

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

或更改ActiveForm::$fieldConfig配置:

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

  • 它还会禁用模型验证的帮助错误. (5认同)

小智 9

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



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

Run Code Online (Sandbox Code Playgroud)

  • 你也可以输入''tag'=> false`,它不会将输入包含在任何内容中. (4认同)

Ole*_*leg 7

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

  • 如果只需要为一个字段移除包装器,这是最好的解决方案。 (2认同)