SignupForm.php 1.18 KB
Newer Older
1
<?php
2
namespace frontend\models;
3 4 5 6 7 8 9 10 11 12

use common\models\User;
use yii\base\Model;
use Yii;

/**
 * Signup form
 */
class SignupForm extends Model
{
13 14 15
    public $username;
    public $email;
    public $password;
16

17 18 19 20 21 22 23 24 25 26
    /**
     * @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],
27

28 29 30 31
            ['email', 'filter', 'filter' => 'trim'],
            ['email', 'required'],
            ['email', 'email'],
            ['email', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This email address has already been taken.'],
32

33 34 35 36
            ['password', 'required'],
            ['password', 'string', 'min' => 6],
        ];
    }
37

38 39 40 41 42 43 44 45 46 47 48 49 50
    /**
     * Signs user up.
     *
     * @return User|null the saved model or null if saving fails
     */
    public function signup()
    {
        if ($this->validate()) {
            return User::create($this->attributes);
        }

        return null;
    }
51
}