Hi, I’m new on Yii and I’m trying to build a registration script.
Firstly, I define a new Controller (AuthController), and I write an action actionRegister. Then, I built the CFormModel and the Active Record for validate and save data in my database. Here’s the actionRegister of the AuthController:
public function actionRegister()
{
if(isset($_GET['AuthRegister']))
{
$model = new RegisterForm;
echo 'instantiate';
$model->attributes=$_GET['AuthRegister'];
if($model->validate() && $model->register())
{
echo 'ok';
return;
}
}
echo 'error';
}
But, when running, instantiate never comes.
RegisterForm code:
class RegisterForm extends CFormModel
{
public $username;
public $password;
public $email;
public $device_id;
public $firstName;
public $secondName;
public $os_type;
private $_identity;
public function rules()
{
return array(
array('username, password, email, firstName, secondName, device_id', 'required'),
array('os_type', 'validateos');
array('email', 'email'),
array('password', 'authenticate'),
);
}
public function validateos($attribute, $params)
{
echo $this->hasErrors();
$OS_TYPE_ANDROID = Yii::app()->params['os_android'];
$OS_TYPE_IOS = Yii::app()->params['os_ios'];
if(!$this->hasErrors())
{
if($this->os_type!==$OS_TYPE_ANDROID && $this->os_type!==$OS_TYPE_IOS)
{
$this->addError('os_type','No OS-type provided.');
}
}
}
/**
* Authenticates the password.
* This is the 'authenticate' validator as declared in rules().
*/
public function authenticate($attribute,$params)
{
echo 'authenticate';
if(!$this->hasErrors())
{
$this->_identity=new UserIdentity($this->username,$this->password,$this->email,$this->firstName,$this->secondName,$this->os_type,$this->device_id);
if(!$this->_identity->authenticate())
$this->addError('password','Already exists');
}
}
public function register()
{
if($this->_identity===null)
{
$this->_identity=new UserIdentity($this->username,$this->password,$this->email,$this->firstName,$this->secondName,$this->os_type,$this->device_id);
}
if($this->_identity->errorCode===UserIdentity::ERROR_NONE && !($this->_identity->authenticate()))
{
$user = new RegisterModel;
$user->username = $this->username;
$user->password = $this->password;
$user->email = $this->email;
$user->firstName = $this->firstName;
$user->secondName = $this->secondName;
$user->os_type = $this->os_type;
$user->device_id = $this->device_id;
$user->save();
return true;
}
else
return false;
}
}
Does anyone can tell me something wrong of my code? I search for hours and I don’t see any error.