SignupForm.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace frontend\forms;
  3. use common\forms\MemberForm;
  4. use common\helpers\ArrayHelper;
  5. use common\models\member\Member;
  6. /**
  7. * Class SignupForm
  8. * @package frontend\models
  9. */
  10. class SignupForm extends MemberForm
  11. {
  12. public $username;
  13. public $email;
  14. public $password;
  15. /**
  16. * {@inheritdoc}
  17. */
  18. public function rules()
  19. {
  20. return ArrayHelper::merge(parent::rules(), [
  21. [['username', 'email'], 'trim'],
  22. [['email', 'username', 'password'], 'required'],
  23. ['username', 'string', 'min' => 2, 'max' => 20],
  24. ['email', 'email'],
  25. ['email', 'string', 'max' => 255],
  26. ['password', 'string', 'min' => 6, 'max' => 20],
  27. ]);
  28. }
  29. public function attributeLabels()
  30. {
  31. return [
  32. 'username' => '登录账号',
  33. 'password' => '登录密码',
  34. 'email' => '电子邮箱',
  35. ];
  36. }
  37. /**
  38. * 注册
  39. *
  40. * @return Member|null
  41. * @throws \yii\base\Exception
  42. */
  43. public function signup()
  44. {
  45. $user = new Member();
  46. $user->username = $this->username;
  47. $user->email = $this->email;
  48. $user->setPassword($this->password);
  49. $user->generateAuthKey();
  50. return $user->save() ? $user : null;
  51. }
  52. }
粤ICP备19079148号