<?php
/*Контроллер*/
class LoginController extends Controller
{
public function actionRegister()
{
$model = new RegisterForm;
if(!empty($_POST['RegisterForm']))
{
$model->attributes = $_POST['RegisterForm'];
if($model->validate() && $model->register())
$this->redirect($this->createUrl('site/index'));
}
$this->render('register', array('model'=>$model));
}
}
/*Модель Регистрации*/
class RegisterForm extends CFormModel
{
public $email;
public $password, $password2;
public function rules()
{
return array(
array('email, password, password2', 'required', 'message'=>'Поле обязательно для заполнения'),
array('email', 'email', 'message'=>'Неверный формат Email'),
array('password', 'compare', 'compareAttribute'=>'password2', 'message'=>'Пароли не совпадают'),
);
}
public function register()
{
if(!$this->hasError())
{
//Добавляем в бд
$model = new User;
$model->user_email = $this->email;
$model->user_password = CPasswordHelper::hashPassword($this->password);
$model->save();
}
else
return false;
}
}
/*Модель User*/
class User extends CActiveRecord
{
public function tableName()
{
return 'user';
}
public function rules()
{
return array(
array('user_email, user_password', 'required'),
array('user_email', 'length', 'max'=>50),
array('user_password', 'length', 'max'=>60),
);
}
public function relations()
{
return array();
}
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}