Class frontend\models\SignupForm

Inheritancefrontend\models\SignupForm » yii\base\Model
Source Code https://github.com/yiisoft/yii2-app-advanced/blob/master/frontend/models/SignupForm.php

Signup form

Public Methods

Hide inherited methods

Method Description Defined By
rules() frontend\models\SignupForm
signup() Signs user up. frontend\models\SignupForm

Protected Methods

Hide inherited methods

Method Description Defined By
sendEmail() Sends confirmation email to user frontend\models\SignupForm

Property Details

Hide inherited properties

$email public property
public $email null
$password public property
public $password null
$username public property
public $username null

Method Details

Hide inherited methods

rules() public method

public void rules ( )

                public function rules()
{
    return [
        ['username', 'trim'],
        ['username', 'required'],
        ['username', 'unique', 'targetClass' => '\common\models\User', 'message' => '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' => 'This email address has already been taken.'],
        ['password', 'required'],
        ['password', 'string', 'min' => Yii::$app->params['user.passwordMinLength']],
    ];
}

            
sendEmail() protected method

Sends confirmation email to user

protected boolean sendEmail ( $user )
$user common\models\User

User model to with email should be send

return boolean

Whether the email was sent

                protected function sendEmail($user)
{
    return Yii::$app
        ->mailer
        ->compose(
            ['html' => 'emailVerify-html', 'text' => 'emailVerify-text'],
            ['user' => $user]
        )
        ->setFrom([Yii::$app->params['supportEmail'] => Yii::$app->name . ' robot'])
        ->setTo($this->email)
        ->setSubject('Account registration at ' . Yii::$app->name)
        ->send();
}

            
signup() public method

Signs user up.

public boolean signup ( )
return boolean

Whether the creating new account was successful and email was sent

                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();
    $user->generateEmailVerificationToken();
    return $user->save() && $this->sendEmail($user);
}