1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- <?php
- namespace frontend\models;
- use yii\base\Model;
- use common\models\User;
- /**
- * Signup form
- */
- class SignupForm extends Model
- {
- public $username;
- public $email;
- public $password;
- /**
- * @inheritdoc
- */
- 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' => 6],
- ];
- }
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'username' => '姓名',
- 'name' => '姓名',
- 'email' => '电子邮件',
- 'password' => '密码',
- 'body' => '内容',
- ];
- }
- /**
- * Signs user up.
- *
- * @return User|null the saved model or null if saving fails
- */
- 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();
-
- return $user->save() ? $user : null;
- }
- }
|