【问题标题】:Yii2 form validating but not submitting to send emailYii2 表单验证但未提交以发送电子邮件
【发布时间】:2018-09-03 16:29:40
【问题描述】:

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

见下表:

<?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-&gt;session-&gt;setFlash() 仍然没有发生任何事情。它只是没有响应。

【问题讨论】:

  • 你怎么知道它正在验证?你试过寻找错误吗?看起来模型没有验证,请尝试在 else 部分使用var_dump($model-&gt;errors); exit; 检查

标签: php yii yii2 yii2-advanced-app yii-form


【解决方案1】:

显然,您有一个错误,但您没有注意到它,因为您正在渲染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-&gt;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>

【讨论】:

  • 哦,是的。谢谢..我错过了。但改正后。它仍然是相同的按钮仍然没有响应
  • 哪个按钮没有响应?您说表单正在提交并且在模型验证时没有发送电子邮件?您现在面临什么问题,控制台@JamesD 中是否有任何错误
  • 我检查了使用 gii 为 CRUD 生成的代码(表单),并在提交按钮周围看到了一个 if 语句。所以我只是模仿在相应的控制器函数中写了我想要的东西。现在它可以正常工作了。谢谢@Muhammad Omer Aslam。
  • 好吧,很高兴知道它现在有效,所以将答案标记为正确。 @JamesD
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-25
  • 1970-01-01
  • 2013-05-04
  • 2015-03-28
  • 2016-07-26
  • 1970-01-01
相关资源
最近更新 更多