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