SignupForm.php 1.87 KB
<?php
    
    namespace frontend\models;
    
    use common\models\Author;
    use yii\base\Model;
    
    /**
     * Signup form
     */
    class SignupForm extends Model
    {
        public $name;
        public $secondname;
        public $phone;
        public $email;
        public $password;
        public $create;
        
        /**
         * {@inheritdoc}
         */
        public function rules()
        {
            return [
                [
                    'name',
                    'trim',
                ],
                [
                    ['name', 'secondname', 'email', 'phone', 'password'],
                    'required',
                ],
                [
                    ['name', 'secondname', 'email', 'phone', 'password'], 'string'
                ],
                [
                    'email', 'email'
                ],
                [
                    'create', 'boolean'
                ],
                [
                    'email',
                    'unique',
                    'targetClass' => '\common\models\Author',
                    'message'     => 'Такой email уже существует',
                ],
            ];
        }
        
        /**
         * Signs user up.
         *
         * @return Author|null the saved model or null if saving fails
         */
        public function signup()
        {
            if (!$this->validate()) {
                return null;
            }
            
            $user = new Author();
            $user->name = $this->name;
            $user->secondname = $this->secondname;
            $user->email = $this->email;
            $user->phone = $this->phone;
            $user->setPassword($this->password);
            $user->generateAuthKey();
            $user->status = Author::STATUS_ACTIVE;
            return $user->save() ? $user : null;
        }
    }