cakePHP验证在更新时被绕过

时间:2012-07-22 05:55:13

标签: php validation cakephp

我有一个个人资料页面,当用户尝试更新他们的页面配置文件,并且输入未根据验证规则进行验证时,它仍然继续保存(或至少输出成功消息)但没有数据然后存储,它将恢复为原始值。只有在传递验证规则时才会存储值。

我不确定如何修复此问题,因为我的验证规则看起来是正确的。有什么想法吗?

验证规则

 public $validate = array(
    "username" => array(
        "email" => array(
            "rule" => "email",
            "message" => "The username must be a valid email address."
        ),
        "unique" => array(
            "rule" => "isUnique",
            "message" => "This username has already been registered."
        )
    ),
    "password" => array(
        "alphaNumeric" => array(
            "rule" => "alphaNumeric",
            "message" => "The password can only contain alpha-numeric characters"
        ),
        "between" => array(
            "rule" => array("between",8,12),
            "message" => "The password must contain between 8 - 12 characters."
        )
    ),
    "company" => array(
        "rule" => "notEmpty",
        "message" => "Please provide a company name"
    ),
    "first_name" => array(
        "rule" => "notEmpty",
        "message" => "Please provide the contact person's first name"
    ),
    "last_name" => array(
        "rule" => "notEmpty",
        "message" => "Please provide the contact person's last name"
    ),
    "telephone" => array(
        "numeric" => array(
            "rule" => "numeric",
            "message" => "The telephone number must be numeric"
        ),
        "maxLength" => array(
            "rule" => array("maxLength",10),
            "message" => "Your telephone umber must be 10 numbers."
        ) 
    ),
    "fax" => array(
        "numeric" => array(
            "rule" => "numeric",
            "message" => "The fax number must be numeric"
        ),
        "maxLength" => array(
            "rule" => array("maxLength",10),
            "message" => "Your fax umber must be 10 numbers."
        )
    ),
    "user_type_id" => array(
        "rule" => "numeric",
        "message" => "Please select a user type"
    ),
    "user_status_id" => array(
        "rule" => "numeric",
        "message" => "Please select the users status."
    )
);

控制器方法:

 public function profile() {
    if($this->request->is('post') || $this->request->is('put')) {
        if($this->Auth->user("id") == $this->request->data['User']['id']) {
            $this->User->save($this->request->data);
            $this->Session->setFlash('Your profile has been updated','default',array('class'=>'success'));
        } else {
            $this->Session->setFlash("An error has occured updating your profile.");
        }
    }
    $this->request->data = $this->User->read(null,$this->Auth->user("id"));
}

2 个答案:

答案 0 :(得分:1)

问题出在你的if块上。 $this->User->save($this->request->data);

周围没有

所以你需要

if ($this->User->save($this->request->data)) {
    // set good flash
} else {
    // else set bad flash
}

然后你需要一个当Auth->用户('id')不等于post数据时(或者如果你要给出一个通用消息,则将两者合并为1 if语句)。

答案 1 :(得分:1)

您的验证可能正常运行。我认为问题是由于以下逻辑:

if($this->Auth->user("id") == $this->request->data['User']['id']) {
    $this->User->save($this->request->data);
    $this->Session->setFlash('Your profile has been updated','default',array('class'=>'success'));
} else {
    $this->Session->setFlash("An error has occured updating your profile.");
}

if语句仅检查当前登录的用户ID是否与表单中提交的用户ID匹配。如果id匹配,它会尝试保存记录。然后它将执行该行。

因此,无论是否保存验证调用,它仍将移至下一行$this->Session->setFlash('Your profile has been updated','default',array('class'=>'success'));。这就是为什么每次都更新配置文件的原因。

你可能想要类似的东西:

if($this->Auth->user("id") == $this->request->data['User']['id']) {
    if ($this->User->save($this->request->data)) {
        $this->Session->setFlash('Your profile has been updated','default',array('class'=>'success'));
    } else {
        $this->Session->setFlash("An error has occured updating your profile.");
    }
} else {
    this->Session->setFlash("This is not your profile.");
}