简体   繁体   English

cakephp验证规则未保留

[英]cakephp Validation rules aren't holding on submit

User Model 用户模型

//...

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."
        )
    ),

//...

User Controller 用户控制器

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

On submit, my form will always validate the username field as false (triggering its corresponding message), no matter what is in the text. 提交时,无论文本中的内容是什么,我的表单都会始终将username段验证为false(触发其相​​应的消息)。 The remaining fields are also not validated. 其余字段也未验证。 If I remove the username validation rules all together, the remaining fields are still not validated. 如果我一起删除username验证规则,则其余字段仍未验证。 (Form is submitted as though validated) (提交表格就像经过验证一样)

What am I missing here? 我在这里想念什么?

Before manually calling $this->User->validates() , you must call the model's set method and provide the form's data. 在手动调用$this->User->validates() ,必须调用模型的set方法并提供表单的数据。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM