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

7 years ago
<?php
namespace frontend\models;
3 years ago
use yii\base\Exception;
7 years ago
use yii\base\Model;
3 years ago
use core\entities\user\User;
7 years ago
use Yii;
/**
* Signup form
*/
class SignupForm extends Model
{
3 years ago
public string $username;
public string $email;
public string $password;
7 years ago
/**
* @inheritdoc
*/
3 years ago
public function rules(): array
7 years ago
{
return [
['username', 'trim'],
['username', 'required'],
3 years ago
['username', 'unique', 'targetClass' => User::class, 'message' => Yii::t('auth', 'This username has already been taken.')],
7 years ago
['username', 'string', 'min' => 2, 'max' => 255],
['email', 'trim'],
['email', 'required'],
['email', 'email'],
['email', 'string', 'max' => 255],
3 years ago
['email', 'unique', 'targetClass' => User::class, 'message' => Yii::t('auth', 'This email address has already been taken.')],
7 years ago
['password', 'required'],
['password', 'string', 'min' => 6],
];
}
/**
3 years ago
* @return User|null
* @throws Exception
7 years ago
*/
3 years ago
public function signup(): ?User
7 years ago
{
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;
}
}