Yii2 表单验证但不提交发送电子邮件

Yii2 form validating but not submitting to send email

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

见下表:

<?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() 仍然没有任何反应。它只是没有响应。

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

<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>

虽然在对 message 字段调用 $model->validate() 时出现验证错误,但它无法显示,因为它将错误分配给表单中使用的属性字段但显然表单中没有名称为 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>