R.Minod
R.Minod

Reputation: 33

Form not submitting in Yii 2

When i clicked my submit button in form it won't happen anything. im try to fill the form and submit but it wont submit. actually it doesn't happen anything. Yii 2.0

This is my userForm View.

<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;     
$this->title = 'User Form';
?>   
<?php 
    if (Yii::$app->session->hasFlash('success')) {
        # code...
        echo Yii::$app->session->getFlash('success');
    }
?>

<!-- to start the form -->
<?php $form = ActiveForm::begin(); ?>

<!-- text fields -->
<?= $form->field($model,'name'); ?>
<?= $form->field($model,'email'); ?>

<!-- button -->
<?= Html::submitButton('Submit',['class'=>'btn btn-success']); ?>

This my actionUser Controller in siteController.php.

public function actionUser()
    {
        $model = new UserForm;

        if($model->load(Yii::$app->request->post()) && $model->validate()){
            //set flash data
            Yii::$app->session->setFlash('success','Successfully entered !..');
        }

        return $this->render('userForm',['model'=>$model]);
    }

This is my UserForm model.

<?php 

namespace app\models; 

use yii\base\Model;


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

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

Upvotes: 0

Views: 2126

Answers (1)

user206
user206

Reputation: 1105

This problem occurs when you have missing ActiveForm::end() call:

Add this at the end of your form:

<?php ActiveForm::end() ?>

Upvotes: 2

Related Questions