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 string $email ''
$password public property
public string $password ''
$username public property
public string $username ''

Method Details

Hide inherited methods

rules() public method

public rules ( )

                public function rules(): array
{
    return [
        ['username', 'trim'],
        ['username', 'required'],
        ['username', 'unique', 'targetClass' => User::class, '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' => User::class, '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 ( \yii\mail\MailerInterface $mailer, common\models\User $user, string $supportEmail, string $appName )
$mailer \yii\mail\MailerInterface

The mailer component.

$user common\models\User

User model to which the email should be sent.

$supportEmail string

The support email address.

$appName string

The application name.

return boolean

Whether the email was sent.

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

            
signup() public method

Signs user up.

public boolean|null signup ( \yii\mail\MailerInterface $mailer, string $supportEmail, string $appName )
$mailer \yii\mail\MailerInterface

The mailer component.

$supportEmail string

The support email address.

$appName string

The application name.

return boolean|null

Whether the creating new account was successful and email was sent.

                public function signup(MailerInterface $mailer, string $supportEmail, string $appName): bool|null
{
    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($mailer, $user, $supportEmail, $appName);
}