cakephp验证规则不持续提交

时间:2013-01-03 03:21:27

标签: php cakephp

用户模型

//...

public $validate = array(
    "username" => array(
        "alpha-numeric" => array(
            'rule'     => 'alphaNumeric',
            'required' => true,
            'message'  => 'Username must contain only numbers and letters.'                 
        ),
        "between" => array(
            "rule" => array("between", 4, 25),
            "message" => "Username must contain between 4 to 25 characters."
        )
    ),

//...

用户控制器

public function index() {
    debug($this->data); // displays values as expected
    debug($this->User->validates()); // always false, unless I remove alphanumeric rule
}

在提交时,我的表单将始终将username字段验证为false(触发其相​​应的消息),无论文本中是什么。其余字段也未经过验证。如果我一起删除username验证规则,则其余字段仍未验证。 (表格提交时已经过验证)

我在这里缺少什么?

1 个答案:

答案 0 :(得分:4)

在手动调用$this->User->validates()之前,您必须调用模型的set方法并提供表单的数据。

public function index() {
    $this->User->set($this->request->data); // Only way the next method will work.
    debug($this->User->validates());
}