1

我是 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/查看/会员/register.ctp

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

1 回答 1

2

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

/app/Model/MemberModel.php

应该

/app/Model/Member.php

控制器会寻找Member.php,如果找不到,它会尝试默认$validate行为AppModel

于 2013-07-28T01:59:13.407 回答