dqg2269 2013-04-24 04:13
浏览 17
已采纳

CakePHP - 密码确认不允许用户提交注册

I'm trying to set up validation for user registration but I'm having troubles. When I only have the email,role and password fields in the $validation array (remove the others) it works and will save a new user. When I try to add the other fields it fails and gives the flash message error "The user could not be saved. Please, try again."

I'm pretty sure it's the re_password check. When I remove the validation for that it works. However, the re_password validation does display an error when the passwords are different, so I'm not sure where to look

Here's my users table

id  |  email  |  password  |  location  | website  | role  | created |  modified

Here's the validation requirements. To get it to save a new user I have to remove everything but email, password and role.

public $validate = array(
    'email' => 'email'
    ,
    'password' => array(
        'required' => array(
            'rule' => array('minLength', '8'),
            'message' => 'A password with a minimum length of 8 characters is required'
        )
    ),
    're_password' => array(
        'required' => array(
            'rule' => array('equalTo', 'password' ), 
            'message' => 'Both password fields must be filled out'
        )
    ),
    'role' => array(
        'valid' => array(
            'rule' => array('inList', array('admin', 'author')),
            'message' => 'Please enter a valid role',
            'allowEmpty' => false
        )
    ),
     'location' => array(
        'valid' => array(
            'rule' => array('notEmpty'),
            'message' => 'Please select a location'
        )
    )
);

Here's the form (the options array is above, figured it's not necessary to show)

    echo $this->Form->input('email');
    echo $this->Form->input('password');
    echo $this->Form->input('re_password', array('type'=>'password', 'label'=>'Re-Enter Password', 'value'=>'', 'autocomplete'=>'off'));
    echo $this->Form->input('location', array('options' => $options, 'label' => 'Select Nearest Location'));
    echo $this->Form->input('website',array('label'=>'Enter your website, such as www.example.com. ')); 
    echo $this->Form->input('role', array('type' => 'hidden', 'default' => 'user'));

Here's the re_password checking function in the User model

function check_user_password($userid) { 
    $salt = Configure::read('Security.salt');
    $this->User->id = $userid;
    $hashed_password = $this->User->field('password');
    // check password  
    if($hashed_password == md5($data['User']['re_password'].$salt)) {  
        return true;
    } else {
        return false;
    }
}   

And finally, here's the add function in UsersController

public function add() {  
        if ($this->request->is('post')) {  
            $this->User->create();     //create initiates a form on User/add.ctp
            if ($this->User->save($this->request->data)) {  //save the form data
                $this->Session->setFlash(__('The user has been saved'));
                $this->redirect(array('controller' => 'demos', 'action' => 'index'));
            } else {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
            }
        }
    }

Please let me know if there's anything else you need to see

  • 写回答

3条回答 默认 最新

  • dsklzerpx64815631 2013-04-24 08:31
    关注

    I believe that your re_passwords valiadtion rule equalTo compares its value to string password and not the actual field. I like to use custom functions for this.

    so try replacing re_passwords rule array

     //'rule' => array('equalTo', 'password' ),
    'rule' => array('equalToField', 'password'),
    

    and declare equalToField function in that model

    function equalToField($array, $field) {
        return strcmp($this->data[$this->alias][key($array)], $this->data[$this->alias][$field]) == 0;
    }
    

    ** Also in the future when you seem to have a problem with validation rules try this in your controllers action (its faster than removing every single rule)

    if ($this->User->save($this->request->data)) {
        ...
    } else {
        debug($this->User->validationErrors);
        ...
    }
    

    I hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 plotBAPC画图出错
  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理