0

我有一个表单,在单击提交按钮时没有提交,但它正在验证。

见下表:

<?php
$form = ActiveForm::begin([
            'id' => 'contact-form',
            'action' => ['site/index'],
            'options' => [
                'class' => 'contact-form wow fadeInUp',
                'data-row-duration' => '1s',
            ]
        ])
?>
<div class="form-validation alert">
    <div class="form-group col-md-12">
        <?=
        $form->field($model, 'name', [
            'options' => ['style' => 'margin:0;padding:0'],
            'inputOptions' => [
                'class' => 'form-control',
                'placeholder' => 'Full Name',
                'autocomplete' => 'off'
            ]
        ])->label(false)
        ?>
    </div>
    <div class="form-group col-md-6">
        <?=
        $form->field($model, 'email', [
            'options' => ['style' => 'margin:0;padding:0'],
            'inputOptions' => [
                'class' => 'form-control',
                'placeholder' => 'Email',
                'autocomplete' => 'off'
            ]
        ])->label(false)
        ?>
    </div>
    <div class="form-group col-md-6">
        <?=
        $form->field($model, 'phone', [
            'options' => ['style' => 'margin:0;padding:0'],
            'inputOptions' => [
                'class' => 'form-control',
                'placeholder' => 'Phone',
                'autocomplete' => 'off'
            ]
        ])->label(false)
        ?>
    </div>
    <div class="form-group col-md-12">
        <?=
        $form->field($model, 'name', [
            'options' => ['style' => 'margin:0;padding:0'],
            'inputOptions' => [
                'class' => 'form-control',
                'placeholder' => 'Message',
                'autocomplete' => 'off',
                'rows' => '5'
            ]
        ])->textarea()->label(false)
        ?>
    </div>
    <div class="form-group col-md-4 col-md-offset-8">
<?=Html::submitButton('Submit', ['class' => 'btn btn-primary', 'name' => 'contact-button']) ?>

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

SiteController/actionIndex:

 public function actionIndex() {
        $model = new ContactForm;
        if( $model->load(Yii::$app->request->post()) && $model->validate() ){
            if( $model->sendEmail(Yii::$app->params['adminEmail']) ){
                Yii::$app->session->setFlash('success', 'Thank you for reaching us. We will respond to you shortly');
            } else{
                Yii::$app->session->setFlash('error', 'Something went wrong. Message not send successfuly');
            }
            return $this->refresh();
        } else{
            return $this->render('index', ['model' => $model]);
        }
    }

注意:我没有收到任何错误。它正在验证,但是在填写表单以单击提交后,该按钮不起作用,我什至使用die()它来代替Yii::$app->session->setFlash()仍然没有发生任何事情。它只是没有响应。

4

1 回答 1

0

显然,您有一个错误,但您没有注意到它,因为您正在渲染该name字段而不是message您的内部字段ActiveForm,我说的是提交按钮之前的最后一个字段。

<div class="form-group col-md-12">
    <?=
    $form->field($model, 'name', [
        'options' => ['style' => 'margin:0;padding:0'],
        'inputOptions' => [
            'class' => 'form-control',
            'placeholder' => 'Message',
            'autocomplete' => 'off',
            'rows' => '5'
        ]
    ])->textarea()->label(false)
    ?>
</div>

并且虽然您在$model->validate()针对该message字段调用时遇到验证错误,但它无法显示,因为它将错误分配给表单中使用的属性字段,但显然表单中没有任何具有该名称message的字段,所以它不显示任何东西。如果您在表单声明之后添加此行,您将立即看到错误

<?= $form->errorSummary($model); ?>

因此,将最后一个字段更改为下面,现在一切正常。

<div class="form-group col-md-12">
    <?=
    $form->field($model, 'message', [
        'options' => ['style' => 'margin:0;padding:0'],
        'inputOptions' => [
            'class' => 'form-control',
            'placeholder' => 'Message',
            'autocomplete' => 'off',
            'rows' => '5'
        ]
    ])->textarea()->label(false)
    ?>
</div>
于 2018-09-03T17:29:56.493 回答