-1

Hello Please I try learn this tutorial

Yii2 tutorial 3

But not working, when I click on submit button not change... if edit it's empty or not...

I thing so <?= Html::submitButton('Submit',['class'=>'btn btn-success']); ?> not detect click, how can check it?

here are my code

in siteControler

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

        if($model->load(Yii::$app->request->post()) && $model->validate()){
            Yii::$app->session->setFlash('success', 'You have entered the data correctly');
        }
        return $this->render('userForm',['model'=>$model]);
    }

models UserForm.php

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

site userForm.php

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

I control from video but I thing so it's 1:1 on video work good but for me, no.. why?

Thanks


my code.. I put into name : adfs and into email: asd@gmail.com and press Submit button, and I want shown on top 'You have entered the data correctly' but not shown... I thing so I can't detect when press button

trip07
  • 157
  • 1
  • 7

1 Answers1

0

Got it you have not closed $form obj, that why you're submit was not triggering. Try this view code site userForm.php

<?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')->textInput() ?>
<?= $form->field($model,'email')->textInput() ?>

<?= Html::submitButton('Submit',['class'=>'btn btn-success']); ?>
<?php ActiveForm::end(); ?>

Let me know if it not working.

Rajesh Pradhan
  • 210
  • 2
  • 10
  • yes work, thanks... please can you give me a theory? when Use $form = ActiveForm::begin(); I must use ActiveForm::end(); too? or? – trip07 Jun 24 '17 at 12:05
  • Yes, it is must. just like
    starts and it must be closed with
    , so $form=ActiveForm::begin i.e
    and ActiveForm::end() i.e
    – Rajesh Pradhan Jun 24 '17 at 16:46