mirror of
https://github.com/elyby/accounts.git
synced 2024-12-26 15:10:22 +05:30
59 lines
1.4 KiB
PHP
59 lines
1.4 KiB
PHP
<?php
|
|
namespace api\models;
|
|
|
|
use common\models\Account;
|
|
use yii\base\Model;
|
|
use Yii;
|
|
|
|
/**
|
|
* Signup form
|
|
*/
|
|
class SignupForm extends Model
|
|
{
|
|
public $username;
|
|
public $email;
|
|
public $password;
|
|
|
|
/**
|
|
* @inheritdoc
|
|
*/
|
|
public function rules()
|
|
{
|
|
return [
|
|
['username', 'filter', 'filter' => 'trim'],
|
|
['username', 'required'],
|
|
['username', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This username has already been taken.'],
|
|
['username', 'string', 'min' => 2, 'max' => 255],
|
|
|
|
['email', 'filter', 'filter' => 'trim'],
|
|
['email', 'required'],
|
|
['email', 'email'],
|
|
['email', 'string', 'max' => 255],
|
|
['email', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This email address has already been taken.'],
|
|
|
|
['password', 'required'],
|
|
['password', 'string', 'min' => 6],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Signs user up.
|
|
*
|
|
* @return Account|null the saved model or null if saving fails
|
|
*/
|
|
public function signup()
|
|
{
|
|
if ($this->validate()) {
|
|
$user = new Account();
|
|
$user->email = $this->email;
|
|
$user->setPassword($this->password);
|
|
$user->generateAuthKey();
|
|
if ($user->save()) {
|
|
return $user;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|