CakePHP模型验证无法正常工作

时间:2017-05-22 06:44:12

标签: cakephp cakephp-2.9

我是CakePHP的新手,并创建了一个提交名字的普通表单。 我的表名是" 寄存器。我创建了一个名为 RegistersController (RegistersController.php)的控制器和一个名为 Register (Register.php)的模型。每次我输入名字后提交,它仍然会显示错误(名字必须),只有在我没有输入任何内容的情况下才提交。接下来,我添加了至少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");

?>

1 个答案:

答案 0 :(得分:2)

你错过了这一行

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

在验证电话会议之前,即 $this->Register->validates()