2014-10-10 96 views
0

我创建了一个表格,我需要用模型和控制器。这里是我的形式如何使用模型和控制器

index.ctp

<?php echo $this->Form->create('Contact',array('url'=>array('controller'=>'contacts','action'=>'add'))); 

echo $this->Form->text('name'); 

模型来验证在CakePHP中检验表单字段:联系.PHP

class Contact extends AppModel 
{ 
     var $name = 'Contact'; 
     var $useTable = false; 

     public $validate = array(
     'name' => array(
      'alphaNumeric' => array(
       'rule'  => 'alphaNumeric', 
       'required' => false, 
       'message' => 'Letters and numbers only' 
      ), 
      'between' => array(
       'rule' => array('between', 5, 15), 
       'message' => 'Between 5 to 15 characters' 
      ) 
     ) 
    ); 
} 

控制器:ContactsController.php

public function add() 
    { 
     $this->Contact->validates(); 

      $this->request->data['Country']['country_name']=$this->request->data['Contact']['country']; 

      $this->Country->saveall($this->request->data); 

      $this->redirect('/Contacts/index/'); 

    } 

我试图通过谷歌搜索做验证,但对我来说似乎很难,所以如果有人可以描述这个过程,这将是一个很大的帮助。我的cakephp版本是2.3.8。我只需要验证这个名称字段,因为当我点击提交它会在窗体中显示此消息。

+0

你的问题是不太清楚。另外请始终提及您的确切CakePHP版本并相应地标记您的问题!这就是说:** http://book.cakephp.org/2.0/en/models/data-validation/validating-data-from-the-controller.html** – ndm 2014-10-10 09:04:44

+0

$ this-> Contact-> validates() ;根据有效或无效的数据返回一个布尔值true或false,并使用$ this-> Contact-> set($ this-> request-> data);在$ this-> Contact-> validates()之前; – Abhishek 2014-10-10 09:09:35

+0

我也编辑了我的问题,当我把$ this-> Contact-> validates()提交给我显示这个错误后 致命错误:调用成员函数validates()对/ opt/lampp中的非对象/htdocs/projects/cake/cakephp/app/Controller/ContactsController.php 74行..请让我知道如果你需要更多的输入从我 – Ron 2014-10-10 09:14:11

回答

1

控制器代码应该是这样的 验证CakePHP中的过程就像是

1) as you have defined validation rules in CakePHP model public `$validates = array();` 

2) when ever you do a save on particular model directly or through any association 
a callback method beforeValidate for that model gets called to validate the data which is being saved. 

3) once the data is validated then beforeSave callback is called after this save method is called. 

4) we can also validate the form input fields in controller using $this->Model->validates() but then while saving we have to disable the beforeValidate callback by doing 

$this->Model->save($data,array('validate'=>false)); 

否则你将最终验证同样的数据两次

控制器代码应该是有点这样。

public function add() { 
     // here we are checking that the request is post method 
     if ($this->request->is('post')) { 
       $this->request->data['Country']['country_name'] 
           = $this->request->data['Contact']['country']; 
       // here we are saving data 
      if ($this->Contact->saveAll($this->request->data)) { 

       //here we are setting a flash message for user 
       $this->Session->setFlash('your record has been added','success'); 

       $this->redirect(array('controller'=>'contacts','action' => 'index')); 
      } else { 
       //here we are setting a flash message for user for error if input are not   
       //validated as expected 
       $this->Session->setFlash('sorry we could add your record','error'); 
      } 

     } 

    } 

欲了解更多信息,可以随时参考http://book.cakephp.org/2.0/en/models/callback-methods.html

相关问题