SignupForm.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace frontend\models;
  3. use yii\base\Model;
  4. use common\models\User;
  5. /**
  6. * Signup form
  7. */
  8. class SignupForm extends Model
  9. {
  10. public $username;
  11. public $email;
  12. public $password;
  13. /**
  14. * @inheritdoc
  15. */
  16. public static function tableName()
  17. {
  18. return '{{%user}}';
  19. }
  20. public function rules()
  21. {
  22. return [
  23. [['username', 'auth_key', 'password_hash', 'status', 'created_at', 'updated_at'], 'required'],
  24. [['role'], 'string'],
  25. [[ 'status', 'created_at', 'updated_at','state'], 'integer'],
  26. [['username', 'auth_key', 'email'], 'string', 'max' => 32],
  27. [['password_hash', 'password_reset_token', 'access_token'], 'string', 'max' => 255],
  28. [['username'], 'unique'],
  29. // [['email'], 'unique'],
  30. [['password_reset_token'], 'unique'],
  31. ];
  32. }
  33. /**
  34. * Signs user up.
  35. *
  36. * @return User|null the saved model or null if saving fails
  37. */
  38. public function signup()
  39. {
  40. if (!$this->validate()) {
  41. return null;
  42. }
  43. $user = new User();
  44. $user->username = $this->username;
  45. $user->email = $this->email;
  46. $user->setPassword($this->password);
  47. $user->generateAuthKey();
  48. return $user->save() ? $user : null;
  49. }
  50. }