cakephp phone number validation

有些话、适合烂在心里 提交于 2019-12-12 03:46:35

问题


i am new to cakephp 2.x so i dont know how to do this .. i want to login the user from his email address and phone number..what my intention is if the number in database is this "12345" and the user is trying to login through this number "+12345" he can be login into the system.. i have written a code but i dont know how can i use this or to adjust my code within the auth component as the auth component is autometically logging the user ..

here is my controller

 public function beforeFilter() {
    parent::beforeFilter();


    $this->Auth->authenticate = array(
        'Authenticate.Cookie' => array(
            'fields' => array(
                'username' => 'email',
                'password' => 'password'
            ),
            'userModel' => 'User',
            'scope' => array('User.active' => 1)
        ),
        'Authenticate.MultiColumn' => array(
            'fields' => array(
                'username' => 'email',
                'password' => 'password'
            ),
            'columns' => array('email', 'mobileNo'),
            'userModel' => 'User',
        )
    );
 }




public function login() {


    if ($this->Auth->login() || $this->Auth->loggedIn()) {
        $this->redirect('/users/dashboard');
    }else{
    $this->layout='logindefault';
    $this->set('title_for_layout', 'Account Login');
    /*$this->Auth->logout();
     $cookie = $this->Cookie->read('Auth.User'); */


    if ($this->request->is('post')) {




        if ($this->Auth->login() || $this->Auth->loggedIn()) {
            if ($this->Session->check('Auth.User')){


            $this->_setCookie($this->Auth->user('idUser'));
            $this->redirect('/users/dashboard');

        } }else {
            $this->Session->setFlash('Incorrect Email/Password Combination');
        }
    }}
}

here is the code which i am trying to add ..

    $mobileNo='+123456789';
   if (strpos($mobileNo,'+') !== false) {
      $mobileNo=str_replace("+", "",$mobileNo);
   }

?>


回答1:


The docs ( http://book.cakephp.org/2.0/en/models/data-validation.html#custom-validation-rules ) have examples on custom validation rules.

But your use case is not about validation (for login there is no normal validation, only a "scope" can be applied). Its about customizing the data that goes into login. You can either use the login() method to modify incoming data prior to the Auth->login() call or use the new 2.x way of doing it: using custom auth adapters.

// in your AppController::beforeFilter() method
$this->Auth->authorize = array('PhoneNumber');

and your PhoneNumber adapter should then be called PhoneNumberAuthenticate and be placed in /Controller/Component/Auth.

See https://github.com/ceeram/Authenticate for details on how others adapters work and apply it to your own one.

But since you are also using "email" to login, you might be best of using the replacement option in your login method:

public function login() {
    $this->request->data['User']['email'] = $this->_replace($this->request->data['User']['email']);
    if ($this->Auth->login()) {...}

with _replace() containing your replacment code.




回答2:


i have done this and it works

i add this line just before the $this->Auth->login() $this->request->data['User']['email'] = $mobileNo;

source:string concatenation when user log in Cakephp



来源:https://stackoverflow.com/questions/17338364/cakephp-phone-number-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!