You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
1.4 KiB
59 lines
1.4 KiB
7 years ago
|
<?php
|
||
|
namespace frontend\models;
|
||
|
|
||
|
use yii\base\Model;
|
||
|
use common\models\User;
|
||
|
use Yii;
|
||
|
|
||
|
/**
|
||
|
* 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' => Yii::t('auth', 'This username has already been taken.')],
|
||
|
['username', 'string', 'min' => 2, 'max' => 255],
|
||
|
|
||
|
['email', 'trim'],
|
||
|
['email', 'required'],
|
||
|
['email', 'email'],
|
||
|
['email', 'string', 'max' => 255],
|
||
|
['email', 'unique', 'targetClass' => '\common\models\User', 'message' => Yii::t('auth', 'This email address has already been taken.')],
|
||
|
|
||
|
['password', 'required'],
|
||
|
['password', 'string', 'min' => 6],
|
||
|
];
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 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;
|
||
|
}
|
||
|
}
|