Class frontend\models\SignupForm
| Inheritance | frontend\models\SignupForm » yii\base\Model |
|---|---|
| Source Code | https://github.com/yiisoft/yii2-app-advanced/blob/master/frontend/models/SignupForm.php |
Signup form
Public Properties
| Property | Type | Description | Defined By |
|---|---|---|---|
| string | frontend\models\SignupForm | ||
| $password | string | frontend\models\SignupForm | |
| $username | string | frontend\models\SignupForm |
Public Methods
| Method | Description | Defined By |
|---|---|---|
| rules() | frontend\models\SignupForm | |
| signup() | Signs user up. | frontend\models\SignupForm |
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| sendEmail() | Sends confirmation email to user. | frontend\models\SignupForm |
Property Details
Method Details
| 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']],
];
}
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();
}
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);
}