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

问题描述 投票:0回答:1

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

见下面的表格:

<?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()仍然没有发生任何事情。它只是没有回应。

php yii yii2 yii2-advanced-app yii-form
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 in形式,所以它不显示任何东西。如果在表单声明后添加此行,您将立即看到错误

<?= $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>
© www.soinside.com 2019 - 2024. All rights reserved.