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