phpyiiyii2yii2-advanced-appyii-form

Yii2 form validating but not submitting to send email


I have a form that is not submitting anytime the submit button is clicked but it is validating.

see the form below:

<?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]);
        }
    }

NOTE: I'm not getting any error. it's validating but after filling the form to click on submit, the button doesn't work I even used die() in place of the Yii::$app->session->setFlash() still nothing happened. it is just not responding.


Solution

  • Apparently, you have an error but you are not noticing it because you are rendering the name field instead of the message field inside your ActiveForm, I am talking about the very last field before the submit button.

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

    and although you have a validation error when you call the $model->validate() against the message field but it is unable to display because it assigns the error to the attribute field that is used in the form but apparently there isn't any field with the name message in the form, so it does not display anything. If you add this line after the form declaration you will immediately see the error

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

    So, change the last field to below and everything will work now.

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