cakephp中的验证总是正确的

时间:2013-07-28 01:29:19

标签: php cakephp

我是cakePHP的新手,并试图创建一个简单的用户注册。现在我因为表格的验证两天而陷入困境。验证函数始终返回true,因此表单始终保存到数据库中,即使它是空的。

所以这里是模型,控制器和视图。也许你可以看到我在这里做错了什么?

/app/Model/MemberModel.php

<?php
class Member extends AppModel {
    var $validate = array(
        "username" => array(
            "rule" => "alphaNumeric",
            "allowEmpty" => false
        )
    );
}
?>

/app/Controller/MemberController.php

<?php
class MemberController extends AppController {

    var $components = array("Security");
    var $helpers = array("Html", "Form");

    public function index() {
    }

    public function register() {

        if($this->request->is("post")) {
            Security::setHash("blowfish");
            $this->Member->set($this->request->data);

            debug($this->Member->validates());

            if($this->Member->save(array("password" => Security::hash($this->request->data["Member"]["password"])))) {
                $this->Session->setFlash(__("Saved."));


    } else {
            $this->Session->setFlash(__("Error: ").$this->validationErrors);
        }
    }
}
}
?>

/app/View/Member/register.ctp

<?php
echo $this->Form->create("Member");
echo $this->Form->input("username");
echo $this->Form->input("password");
echo $this->Form->end("Register");
?>

1 个答案:

答案 0 :(得分:2)

哦,我刚刚意识到你的问题。

/app/Model/MemberModel.php

应该是

/app/Model/Member.php

控制器会查找Member.php,如果找不到,它会尝试$validate

中的默认AppModel行为