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 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败