0

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.

Muhammad Omer Aslam
  • 22,976
  • 9
  • 42
  • 68
James D
  • 125
  • 4
  • 14
  • how come you know that it is validating? have you tried looking for errors? what looks like the model is not validating , try checking with `var_dump($model->errors); exit;` in the else part – Muhammad Omer Aslam Sep 03 '18 at 16:33

1 Answers1

0

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>
Muhammad Omer Aslam
  • 22,976
  • 9
  • 42
  • 68
  • Oh yeah. Thanks..I missed that. But after making the correction. It still the same the button still not responding – James D Sep 03 '18 at 21:12
  • which button is not responding? you said the form is submitting and it is not sending the email at the point of model validation? what problem are you facing now, are there any errors in the console @JamesD – Muhammad Omer Aslam Sep 04 '18 at 00:22
  • I checked the code(form) generated for CRUD using gii and saw an if statement around the submit button. So I just imitated that wrote what I want in the corresponding controller function.now it works fine. Thanks @Muhammad Omer Aslam. – James D Sep 04 '18 at 20:26
  • okay good to know it works now, so mark the answer as correct. @JamesD – Muhammad Omer Aslam Sep 04 '18 at 21:30