Yii Forms

Hi Everyone

Im a newbie in YII. Im creating a basic form with 1 inputs, submit them to the database. Im having an error, I need a little help here.

PHP notice

Undefined variable: model

/home/maher/www/chess24com/app/views/user/profile_edit.php(15)

profile_edit.php


 

<div class="form">

<?php echo CHtml::beginForm(); ?>

 

                                                                    

 

    <div class="row">

        <?php echo CHtml::activeLabel($model,'dob'); ?>

        <?php echo CHtml::activeTextField($model,'dob') ?>

    </div>

 

    <div class="row submit">

        <?php echo CHtml::submitButton('Save'); ?>

    </div>

 

<?php echo CHtml::endForm(); ?>

</div><!-- form --> 



UserController




public function actionEditDob() {

            $model = new ChangeDob;

    if(isset($_POST["ChangeDob"])){

        $model->attributes=$_POST["ChangeDob"];

    

    if($model->validate())

            $this->redirect(Yii::app()->user->returnUrl);

    }

    // displays the login form

    $this->render('profile_edit');

    $this->render('profile_edit',array('model'=>$model));

    

    return $model;

        }



Mode :ChangeDob




public function rules()

    {

        return array(

            array('dob', 'required'),

           

        );

    }



Thanks in advance for your help.


    $this->render('profile_edit');

    $this->render('profile_edit',array('model'=>$model));

You need to remove the first line as that one does not send the $model variable…

I removed it and still same problem.

:(

Maybe you should also remove that last line:


return $model;

What’s its purpose anyway?

May I suggest you to begin with Gii’s CRUD code. It gives you standard working controllers, models, and views :)

it still doesn’t work, same problem.

I already took a look in CRUD. and it’s not helping enough.

I need a form example to follow.

Is it giving you the same error or a different one after you remove those two lines? The original error really doesn’t make much sense if you’ve followed the suggestions above.

Put this at the top of profile_edit.php.




<?php $model = new ChangeDob;?>



Now it should look like the following:




<div class="form">

<?php echo CHtml::beginForm(); ?>

 <?php $model = new ChangeDob;?>

                                                                    

 

    <div class="row">

        <?php echo CHtml::activeLabel($model,'dob'); ?>

        <?php echo CHtml::activeTextField($model,'dob') ?>

    </div>

 

    <div class="row submit">

        <?php echo CHtml::submitButton('Save'); ?>

    </div>

 

<?php echo CHtml::endForm(); ?>

</div><!-- form --> 



The whole point of passing the model into the view from the controller is to allow the form fields to persist and to show any errors. I don’t recommend declaring a new model in the view.

Hi

thank you very much, it works in now.

thank you

It works, but this is nor the way to solve your problem and it will not work at all for the update action… you need to create the model in the controller and pass that variable to the view.

Yes you’r right.

I i did it as you said. Thank you all for your support.

::)