Password Confirmation cannot be blank

Hi,

My Yii version is 1.1. I created a model named ‘user’.

// model

class User extends CActiveRecord

{

public $conf_password;





public function rules()


{


    


    return array(


       array('name, password, conf_password', 'required'),           


        array('password', 'length', 'max'=>40),


        array('conf_password', 'length', 'max'=>40),


        array('password', 'compare', 'compareAttribute'=>'conf_password'),


        array('conf_password', 'safe'));





}

}

In Controller class,

    public function actionCreate()


{


	$model=new User;





	// Uncomment the following line if AJAX validation is needed


	// $this->performAjaxValidation($model);





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


	{


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


		if($model->save())


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


	}





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


		'model'=>$model,


	));


}

When I call the create action, I encounter the problem "The Confirm password cannot be blank".

The confirm password is not included in my database table.

Please help me to solve my problem.

Thanks.

Hi Win Naung,

even if the confirm password is not in your database, it is declared in your model ($conf_password) and it also has a validation rule that says it can’t be empty.


class User extends CActiveRecord

{

public $conf_password;     // Here you declare the "confirm password" property


public function rules()

{


return array(

array('name, password, conf_password', 'required'), // here you say it can't be NULL

array('password', 'length', 'max'=>40),

array('conf_password', 'length', 'max'=>40),       // here you say it can't be more than 40 characters

array('password', 'compare', 'compareAttribute'=>'conf_password'),   // here you say that the password must be the same as conf_password

array('conf_password', 'safe'));   // here you say that conf_password can be massively assigned from POST


}

So you can remove that if you don’t need a ‘confirm password’ field in your registration form.

Hope it helped

ciao

B)

Hi Ciao,

Thanks for your quick response.

After I tried to solve the problem for a few hours, I got a solution. I am not sure the solution is correct or not. But I could use the confirm password field without error.

I assigned the confirm password field in the action.

This is what I tried.

public function actionCreate()

{


	$model=new User;





	// Uncomment the following line if AJAX validation is needed


	// $this->performAjaxValidation($model);





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


	{


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


                    


                    // This line is what I added.......


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


		


                    if($model->save())


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


	}





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


		'model'=>$model,


	));


}

Now the confirm password is working. Please I appreciate your suggestion and help.

Thanks so much.

Win Naung

yes, what you wrote will work fine … however, writting :


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

…should be enough. The conf_password is defined as ‘safe’ so it can be massively assigned. Do you mean that if you remove $model->conf_password=$_POST[‘User’][‘conf_password’] it doesn’t work anymore ?

B)

Yes… If I remove the line, my program doesn’t work.

humm … can you post the view you are using ?

In View,

<div class="form">

&lt;?php &#036;form=&#036;this-&gt;beginWidget('CActiveForm', array(


        'id'=&gt;'ts-customers-form',


        'enableAjaxValidation'=&gt;false,


)); ?&gt;





        &lt;p class=&quot;note&quot;&gt;Fields with &lt;span class=&quot;required&quot;&gt;*&lt;/span&gt; are required.&lt;/p&gt;





        &lt;?php echo &#036;form-&gt;errorSummary(&#036;model); ?&gt;





        &lt;table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot;&gt;                


            &lt;tr&gt;


                &lt;td&gt;&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'customers_firstname'); ?&gt;&lt;/td&gt;


                &lt;td&gt;


                    &lt;?php echo &#036;form-&gt;textField(&#036;model,'customers_firstname',array('size'=&gt;60,'maxlength'=&gt;255)); ?&gt;


                    &lt;?php echo &#036;form-&gt;error(&#036;model,'customers_firstname'); ?&gt;


                &lt;/td&gt;


            &lt;/tr&gt;


            &lt;tr&gt;


                &lt;td&gt;&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'customers_lastname'); ?&gt;&lt;/td&gt;


                &lt;td&gt;


                    &lt;?php echo &#036;form-&gt;textField(&#036;model,'customers_lastname',array('size'=&gt;60,'maxlength'=&gt;255)); ?&gt;


                    &lt;?php echo &#036;form-&gt;error(&#036;model,'customers_lastname'); ?&gt;


                &lt;/td&gt;


            &lt;/tr&gt;


                                         


            &lt;tr&gt;


                &lt;td&gt;&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'customers_password'); ?&gt;&lt;/td&gt;


                &lt;td&gt;


                    &lt;?php echo &#036;form-&gt;textField(&#036;model,'customers_password',array('size'=&gt;40,'maxlength'=&gt;40)); ?&gt;


                    &lt;?php echo &#036;form-&gt;error(&#036;model,'customers_password'); ?&gt;


                &lt;/td&gt;


            &lt;/tr&gt;


            &lt;tr&gt;


                &lt;td&gt;&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'customers_conf_password'); ?&gt;&lt;/td&gt;


                &lt;td&gt;


                    &lt;?php echo &#036;form-&gt;textField(&#036;model,'customers_conf_password',array('size'=&gt;40,'maxlength'=&gt;40)); ?&gt;


                    &lt;?php echo &#036;form-&gt;error(&#036;model,'customers_conf_password'); ?&gt;


                &lt;/td&gt;


            &lt;/tr&gt;


            


        &lt;/table&gt;





        &lt;div class=&quot;row buttons&quot;&gt;


                &lt;?php echo CHtml::submitButton(&#036;model-&gt;isNewRecord ? 'Create' : 'Save'); ?&gt;


        &lt;/div&gt;





&lt;?php &#036;this-&gt;endWidget(); ?&gt;





&lt;/div&gt;&#60;&#33;-- form --&#62;

Thanks

Hi Win Naung,

the view refers to attributes that are not defined in your model. For instance I see a field ‘name’ in the validation rule definition, but there is no ‘name’ attribute in the view.

Based on this view, you should write your model like :


class User extends CActiveRecord

{

public $customers_conf_password;     // same name than inside the view


public function rules()

{


return array(

array('name, customers_password, customers_conf_password', 'required'), 

array('customers_password', 'length', 'max'=>40),

array('customers_conf_password', 'length', 'max'=>40),      

array('customers_password', 'compare', 'compareAttribute'=>'customers_conf_password'),   

array('customers_conf_password', 'safe'));  


}

Hope this helps

B)

Hi Raoul,

I am sorry for that I forgot to post my model here. My model is same as to the code you posted.

The error message is still displaying after I removed the code I showed at the previous post.

let me know if you have any idea else.

Thanks

Win Naung

I think that $model->attributes = $_POST["model"] only assigns attributes that are in the database.

Other user defined attributes must be assigned manually

Yes, confirmed. Is this a bug, or a feature?

Hi,

I am still trying to solve that issue. I will get back to you if I have any issue.

Thanks.

I assigned the confirmed password field manually which is defined in the model.

$model->attributes = $_POST[‘model’] statements assigned all data of the database table column fields.

For the user defined attributes which is not in the database column fields, we need to assign manually in the action.

If I was wrong, please guide me and advice me.

Thanks