0

I created a simple form with simple rules declared in model, but when I tried to submit, It is not working, I don't encountered an error also. The expected output should be the session flash, but nothing happened in my case

This is my controller

public function actionCustomer()
{
    $model = new CustomerForm;
    if($model->load(Yii::$app->request->post()) && $model->validate())
    {
        Yii::$app->session->setFlash('success','you entered it correctly');
    }
    return $this->render('customerForm',['model'=>$model]);
    

}

This is my Model

<?php
   namespace app\models;
   use yii\base\Model;

   class CustomerForm extends Model
   {
   public $name;
   public $email;

    public function rules()
    {
       return [
           [['name', 'email'], 'required'],
           ['email', 'email'],
     
        ];
    }

}

This is my view

            <?php

        use yii\helpers\Html;
        use yii\widgets\ActiveForm;


        ?>

        <?php

            if(Yii::$app->session->hasFlash('success')){
                echo Yii::$app->session->getFlash('success');
            }
        ?>

        <?php $form = ActiveForm::begin(); ?>
        <?= $form->field($model,'name'); ?>
        <?= $form->field($model,'email'); ?>
        <?= Html::submitButton('Submit',['class'=>'btn btn-success']); ?>

1 Answers1

0

Controller and Model are fine. I think you just simply missed the end() method of the ActiveForm widget in the View. In this case, missing the closing tag won't render a form tag, thus when you click submit nothing happens.

Call the end() method of the ActiveForm Widget in customerForm.php

<?php ActiveForm::end(); ?>