[Solved] Having trouble with Register Scenario

I am a newbie learning this exciting Framework.

After getting some experience with the blog tutorial, I decided to implement the user management and generated the user model and UserController. As I want to let the users register with the side, I added actionRegister() to the UserController.php


	public function actionRegister()

	{

		$model=new User;

		

		if(isset($_POST['User']))

		{

			$model->attributes=$_POST['User'];

			if($model->save())

				$this->redirect(array('view','id'=>$model->id));

		}

		

		// Calls protected\views\user\register.php 

		$this->render('register',array(

			'model'=>$model,

		));	}	

	

The following code in user\register.php calls the protected\views\user\formregister.php view file.


<?php 

echo $this->renderPartial('formregister', array('model'=>$model)); ?>

And, I modified user.rules() as given here, as I want to do different validation at the time of registration.


	public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array( 

			array('username, password, email', 'required'),

			array('userlevel', 'numerical', 'integerOnly'=>true),

			array('username, password, password2, email', 'length', 'max'=>128),

			array('firstname, lastname, title', 'length', 'max'=>50),

			array('profile', 'safe'),

			

			// added for password comparision during 'actionRegister' 

			array('password2', 'required','on'=>'register'),			

			array('firstname', 'required','on'=>'register'),			

			array('password', 'compare', 'compareAttribute'=>'password2', 'on'=>'register'),

			

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('username, email, profile, firstname, lastname, title, userlevel', 'safe', 'on'=>'search'),

			

			// added to ensure that username is unique 

			array('username', 'unique'),

		);

	}

Finally, I created a link on the login form to link to index.php/user/register

So far so good, and on clicking the link, the site open the user registration form, however, the form does not display password2 and firatname fields as required. (It does not display * after Password2 and Firstname fields.) Also, when I display scenario with <?php echo "Scenario: " .$model->getScenario(); ?>[font="Courier New"][/font], I see insert.

Anyway, I use forced the scenario to "register" with $model->setScenario("register") in register.php and then I started seeing "*" after Password2 and Firstname fields on formregister, however, when I submit the form, the framework does not display error even when these fields are blank nor does it compare password and password2 fields.

I am stuck and don’t know what to do next. Could someone put me on the right track?

$model = new User(‘register’); or $model = new User; $model->scenario = ‘register’;

Are you sure that you are displaying the errors in the form view?




public function actionRegister()

        {

                $model=new User('register');

...



Thanks Omko and Jayrulez. $model=new User(‘register’); solved the problem.