Blame view

frontend/models/SignupForm.php 1.97 KB
4253cbec   root   first commit
1
2
3
  <?php

  namespace frontend\models;

  

aefe93aa   Administrator   big commti
4
  use common\models\Customer;

4253cbec   root   first commit
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  use yii\base\Model;

  use Yii;

  

  /**

   * Signup form

   */

  class SignupForm extends Model

  {

      public $username;

      public $email;

      public $password;

      public $verifyCode;

      public $password_repeat;

      public $surname;

      public $phone;

  

4253cbec   root   first commit
21
22
23
24
25
26
27
28
29
30
31
32
33
34
      /**

       * @inheritdoc

       */

      public function rules()

      {

          return [

              ['username', 'filter', 'filter' => 'trim'],

              ['username', 'required'],

              [['username','surname'], 'string', 'min' => 2, 'max' => 255],

  

              ['email', 'filter', 'filter' => 'trim'],

              ['email', 'required'],

              ['email', 'email'],

              [['email','phone'], 'string', 'max' => 255],

a1a60fa5   Administrator   big commti
35
              ['email', 'unique', 'targetClass' => '\common\models\Customer', 'message' =>  Yii::t('app','message',[

4253cbec   root   first commit
36
37
38
                  'field' => 'Email'

              ])],

  

a1a60fa5   Administrator   big commti
39
              [['phone'], 'unique', 'targetClass' => '\common\models\Customer', 'message' =>  Yii::t('app','message',[

4253cbec   root   first commit
40
41
42
43
44
45
46
47
                  'field' => 'Телефон'

              ])],

  

              ['password_repeat', 'required'],

              ['password_repeat', 'compare', 'compareAttribute'=>'password', 'message'=> Yii::t('app', 'message_match_password') ],

  

              ['password', 'required'],

              ['password', 'string', 'min' => 6],

4253cbec   root   first commit
48
49
50
51
          ];

      }

  

  

5b7adec0   Administrator   big commti
52
53
54
  

  

  

4253cbec   root   first commit
55
      /**

4253cbec   root   first commit
56
57
       * Signs user up.

       *

a1a60fa5   Administrator   big commti
58
       * @return Customer|null the saved model or null if saving fails

4253cbec   root   first commit
59
60
61
62
63
64
65
66
       */

      public function signup()

      {

  

          if (!$this->validate()) {

              return null;

          }

          

aefe93aa   Administrator   big commti
67
          $user = new Customer();

4253cbec   root   first commit
68
69
70
71
72
73
74
75
76
77
          $user->username = $this->username;

          $user->surname = $this->surname;

          $user->email = $this->email;

          $user->phone = $this->phone;

          $user->setPassword($this->password);

          $user->generateAuthKey();

          $user->validate();

          return $user->save() ? $user : null;

      }

  }