linccce

Recovery Model

Jun 10th, 2014
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.47 KB | None | 0 0
  1. class UserRecoveryForm extends CFormModel {
  2.     public $login_or_email, $user_id;
  3.    
  4.     /**
  5.      * Declares the validation rules.
  6.      * The rules state that username and password are required,
  7.      * and password needs to be authenticated.
  8.      */
  9.     public function rules()
  10.     {
  11.         return array(
  12.             // username and password are required
  13.             array('login_or_email', 'required'),
  14.             array('login_or_email', 'match', 'pattern' => '/^[[email protected]\s,]+$/u','message' => UserModule::t("Incorrect symbols (A-z0-9).")),
  15.             // password needs to be authenticated
  16.             array('login_or_email', 'checkexists'),
  17.         );
  18.     }
  19.     /**
  20.      * Declares attribute labels.
  21.      */
  22.     public function attributeLabels()
  23.     {
  24.         return array(
  25.             'login_or_email'=>UserModule::t("username or email"),
  26.         );
  27.     }
  28.    
  29.     public function checkexists($attribute,$params) {
  30.         if(!$this->hasErrors())  // we only want to authenticate when no input errors
  31.         {
  32.             if (strpos($this->login_or_email,"@")) {
  33.                 $user=User::model()->findByAttributes(array('email'=>$this->login_or_email));
  34.                 if ($user)
  35.                     $this->user_id=$user->id;
  36.             } else {
  37.                 $user=User::model()->findByAttributes(array('username'=>$this->login_or_email));
  38.                 if ($user)
  39.                     $this->user_id=$user->id;
  40.             }
  41.            
  42.             if($user===null)
  43.                 if (strpos($this->login_or_email,"@")) {
  44.                     $this->addError("login_or_email",UserModule::t("Email is incorrect."));
  45.                 } else {
  46.                     $this->addError("login_or_email",UserModule::t("Username is incorrect."));
  47.                 }
  48.         }
  49.     }
  50.    
  51. }
Advertisement
Add Comment
Please, Sign In to add comment