1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\basic\forms; |
4
|
|
|
|
5
|
|
|
use app\basic\models\UserModels; |
6
|
|
|
use yii\base\Model; |
7
|
|
|
use yii\helpers\Yii; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* SignupForm is the model behind the signup form Web Application Basic. |
11
|
|
|
**/ |
12
|
|
|
class SignupForm extends Model |
13
|
|
|
{ |
14
|
|
|
public $username; |
15
|
|
|
public $email; |
16
|
|
|
public $password; |
17
|
|
|
public $verifyCode; |
18
|
|
|
|
19
|
|
|
private $_User; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* rules |
23
|
|
|
* |
24
|
|
|
* @return array the validation rules. |
25
|
|
|
**/ |
26
|
|
|
public function rules() |
27
|
|
|
{ |
28
|
|
|
return [ |
29
|
|
|
['username', 'trim'], |
30
|
|
|
['username', 'required'], |
31
|
|
|
['username', 'unique', 'targetClass' => 'app\basic\models\UserModels', 'message' => 'This username has already been taken.'], |
32
|
|
|
['username', 'string', 'min' => 2, 'max' => 255], |
33
|
|
|
['email', 'trim'], |
34
|
|
|
['email', 'required'], |
35
|
|
|
['email', 'email'], |
36
|
|
|
['email', 'string', 'max' => 255], |
37
|
|
|
['email', 'unique', 'targetClass' => 'app\basic\models\UserModels', 'message' => 'This email address has already been taken.'], |
38
|
|
|
['password', 'required'], |
39
|
|
|
['password', 'string', 'min' => 6], |
40
|
|
|
// verifyCode needs to be entered correctly |
41
|
|
|
//['verifyCode', \yii\captcha\CaptchaValidator::class], |
42
|
|
|
]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* atributeLabels |
47
|
|
|
* Translate Atribute Labels. |
48
|
|
|
* |
49
|
|
|
* @return array customized attribute labels. |
50
|
|
|
**/ |
51
|
|
|
public function attributeLabels() |
52
|
|
|
{ |
53
|
|
|
return [ |
54
|
|
|
'email' => Yii::t('basic', 'Email'), |
55
|
|
|
'username' => Yii::t('basic', 'Username'), |
56
|
|
|
'password' => Yii::t('basic', 'Password'), |
57
|
|
|
'verifyCode' => Yii::t('basic', 'VerifyCode'), |
58
|
|
|
]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* signup |
63
|
|
|
* Signs user up. |
64
|
|
|
* |
65
|
|
|
* @return UserModels|null the saved model or null if saving fails. |
66
|
|
|
**/ |
67
|
|
|
public function signup() |
68
|
|
|
{ |
69
|
|
|
if (!$this->validate()) { |
70
|
|
|
return null; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$this->_User = new UserModels(); |
74
|
|
|
$this->_User->username = $this->username; |
75
|
|
|
$this->_User->email = $this->email; |
76
|
|
|
$this->_User->setPassword($this->password); |
77
|
|
|
$this->_User->generateAuthKey(); |
78
|
|
|
|
79
|
|
|
return $this->_User->save() ? $this->_User : null; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|