73 lines
1.4 KiB
PHP
Executable File
73 lines
1.4 KiB
PHP
Executable File
<?php
|
|
|
|
namespace frontend\models;
|
|
|
|
use Yii;
|
|
use yii\base\Model;
|
|
use common\models\User;
|
|
|
|
/**
|
|
* Signup form
|
|
*/
|
|
class SignupForm extends Model
|
|
{
|
|
public $username;
|
|
public $email;
|
|
public $password;
|
|
|
|
|
|
/**
|
|
* @inheritdoc
|
|
*/
|
|
public function rules()
|
|
{
|
|
return [
|
|
['username', 'trim'],
|
|
['username', 'required'],
|
|
['username', 'unique', 'targetClass' => '\common\models\User', 'message' => '该用户名已经有人注册了.'],
|
|
['username', 'string', 'min' => 2, 'max' => 255],
|
|
|
|
['email', 'trim'],
|
|
['email', 'required'],
|
|
['email', 'email'],
|
|
['email', 'string', 'max' => 255],
|
|
['email', 'unique', 'targetClass' => '\common\models\User', 'message' => '该邮箱地址已经有人使用了.'],
|
|
|
|
['password', 'required'],
|
|
['password', 'string', 'min' => 6],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @inheritdoc
|
|
*/
|
|
public function attributeLabels()
|
|
{
|
|
return [
|
|
'username' => Yii::t('frontend', 'Username'),
|
|
'password' => Yii::t('frontend', 'Password'),
|
|
'email' => Yii::t('frontend', 'Email'),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Signs user up.
|
|
*
|
|
* @return User|null the saved model or null if saving fails
|
|
*/
|
|
public function signup()
|
|
{
|
|
if (!$this->validate()) {
|
|
return NULL;
|
|
}
|
|
|
|
$user = new User();
|
|
$user->username = $this->username;
|
|
$user->email = $this->email;
|
|
$user->setPassword($this->password);
|
|
$user->generateAuthKey();
|
|
|
|
return $user->save() ? $user : NULL;
|
|
}
|
|
}
|