I’m not receiving any errors but I’m not able to insert a record. My controller has the following and the critical part is where I call $model->addUser():
public function actionRegister()
{
$model=new RegisterForm;
// if it is ajax validation request
if(isset($_POST['ajax']) && $_POST['ajax']==='register-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
// collect user input data
if(isset($_POST['RegisterForm']))
{
$model->attributes=$_POST['RegisterForm'];
// validate user input and redirect to the previous page if valid
if($model->validate() && $model->checkEmail() && $model->checkUserName()){
$model->addUser();
$model->sendConfirmation();
$msg = array('message' => $model->setConfirmMsg(), 'email' => $model->email);
$this->render('confirmed', $msg);
} else {
//display errors
$this->render('register',array('model'=>$model));
}
} else {
// display the register form
$this->render('register',array('model'=>$model));
}
}
I know there is an issue where you can’t save a record unless you specify every single field in the code reference above, therefore I have added the following to my User model:
public function rules()
{
return array(
array('userName, email, password, pic, signupDate', 'safe', 'on'=>'search'),
);
}
[/size][/font][color=#666600][font=monospace][size=2]{[/size][/font][/color][font=monospace][size=2]
$model[/size][/font][color=#666600][font=monospace][size=2]->[/size][/font][/color][font=monospace][size=2]attributes[/size][/font][color=#666600][font=monospace][size=2]=[/size][/font][/color][font=monospace][size=2]$_POST[/size][/font][color=#666600][font=monospace][size=2][[/size][/font][/color][color=#008800][font=monospace][size=2]'RegisterForm'[/size][/font][/color][color=#666600][font=monospace][size=2]];[/size][/font][/color][font=monospace][size=2]
[/size][/font][color=#880000][font=monospace][size=2]// validate user input and redirect to the previous page if valid[/size][/font][/color][font=monospace][size=2]
[/size][/font][color=#000088][font=monospace][size=2]if[/size][/font][/color][color=#666600][font=monospace][size=2]([/size][/font][/color][font=monospace][size=2]$model[/size][/font][color=#666600][font=monospace][size=2]->[/size][/font][/color][font=monospace][size=2]validate[/size][/font][color=#666600][font=monospace][size=2]()[/size][/font][/color][font=monospace][size=2] [/size][/font][color=#666600][font=monospace][size=2]&&[/size][/font][/color][font=monospace][size=2] $model[/size][/font][color=#666600][font=monospace][size=2]->[/size][/font][/color][font=monospace][size=2]checkEmail[/size][/font][color=#666600][font=monospace][size=2]()[/size][/font][/color][font=monospace][size=2] [/size][/font][color=#666600][font=monospace][size=2]&&[/size][/font][/color][font=monospace][size=2] $model[/size][/font][color=#666600][font=monospace][size=2]->[/size][/font][/color][font=monospace][size=2]checkUserName[/size][/font][color=#666600][font=monospace][size=2]()){[/size][/font][/color][font=monospace][size=2]
$model[/size][/font][color=#666600][font=monospace][size=2]->[/size][/font][/color][font=monospace][size=2]save[/size][/font][color=#666600][font=monospace][size=2]();[/size][/font][/color][font=monospace][size=2] //other 3 lines of your code
Change your addUser() function in the RegisterForm class into beforeSave() with only 2 lines that assign pic and signup date values.
The reason you cannot see error is because in addUser() you create another "model" to save data. By time addUser() finishes that model dies and error information lost with it.
You are doing the mass assignment of attributes from $_POST to RegisterForm model. So the ‘safe’ ‘on’ rules have to be specified in RegisterForm model. You have added the rules in User model.