CakePHP 模型验证无法正常工作

CakePHP Model validation not working properly

我是 CakePHP 的新手,创建了一个普通表单来提交名字。 我的 table 名称是“registers。我创建了一个名为 RegistersController (RegistersController.php) 的控制器和一个名为 RegistersController.php 的模型=23=]Register (Register.php) 每次我输入名字后提交,它仍然显示错误 (First name is must) 这是应该的仅当我提交时不输入任何内容。接下来我添加了至少 6 个字符的验证。该验证也不起作用。我的意思是,cakephp 没有验证该规则。有人能告诉我我哪里做错了吗?

型号:-

class Register extends AppModel {
//put your code here

//public $useTable = "registers";

public $validate = array(
  'first'=>array(                   
       'minLength' => array(
          'rule' => array('minlength','6'),
          'field' => 'first',
          'message' => 'Minimum 6 characters required'
       ),
       'required' => array(
           'rule'=>array('notEmpty'),
           'required' => true,
           'message' => array('First name is must')
       )        
   )
);

}

控制器:-

class RegistersController extends AppController {

public $uses = array("Register");
//put your code here
public function index() {
    if($this->request->is('post')){
        //echo "Data";           
        if($this->Register->validates()){
            //$this->Register->create();
            //echo "Data validated";
            print_r($this->Register->validationErrors);                
        }else{
            //echo "Data not validated";
            print_r($this->Register->validationErrors);
        } 
    }
}

我的看法如下:-

<?php

echo $this->Form->create('Register');

echo $this->Form->input('first');

echo $this->Form->end("Submit");

?>

你漏掉了这一行

$this->Register->set($this->request->data);

将其放在验证调用之前,即 $this->Register->validates()