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 |
---|---|---|---|
frontend\models\SignupForm | |||
$password | frontend\models\SignupForm | ||
$username | 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 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']],
];
}
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();
}
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);
}