2012-09-12 211 views
2

Iam在cakephp中新建,我需要驗證表單。CakePHP驗證不起作用

這是代碼: 控制器:

<?php 
class TasksController extends AppController { 
    var $name = 'Tasks'; 
    var $helpers = array('Html','Form','Session'); 
    public function index(){ 
    } 
    function add_task() 
    { 
     if(!empty($this->data)) { 
      //print_r($this->data); 
      $this->Task->set($this->data); 
      if ($this->Task->validates()) { 
       // it validated logic 
       //echo "ttt"; 
      } else { 
       // didn't validate logic 
       echo $errors = $this->Task->validationErrors; 
      } 
     } 
    } 
} 
?> 

型號:

<?php 
    class Task extends AppModel 
    { 
     public var $name = 'Task'; 
     var $useDbConfig = 'travanco_erp'; 
     public var $useTable = 'tbl_tasks'; // This model uses a database table 'exmp' 
     public var $validate = array(
        'task_title_mm' => array(
          'rule' => 'notEmpty', 
          'required' => true, 
          'message' => 'The title field is required' 
        ), 
        'task_description_mm' => array(
          'rule' => 'notEmpty', 
          'required' => true, 
          'message' => 'The description field is required' 
        ), 
        'task_from_mm' => array(
          'rule' => 'notEmpty', 
          'required' => true, 
          'message' => 'The from date field is required' 
        ), 
        'task_to_mm' => array(
          'rule' => 'notEmpty', 
          'required' => true, 
          'message' => 'The to date field is required' 
        ) 
      ); 

    } 
?> 

這是視圖:

<div class="employeeForm" style="width:64%; padding:10px 30%;"> 

      <?php echo $this->Form->create('test', array('class'=>'form'));?> 
      <fieldset style="width:36em; padding:0px 0px;"> 
        <div style="width:475px; font-family:Arial, Helvetica, sans-serif; font-size:16px; color:#333333; font-weight:bold; margin-left:20px; margin-top:10px;">Add Task</div> 
        <br/> 
       <?php 
        /*echo $this->Form->input('task_ids_mm',  array( 'div'=>'frm_filed_new', 
                    'error' => array( 'wrap' => 'div', 
                         'class' => 'formerror' 
                        ), 
                    'label' => 'Task ID', 
                   ));*/ 


        echo $this->Form->input('task_title_mm',  array( 'div'=>'frm_filed_new', 
                    'error' => array( 'wrap' => 'div', 
                         'class' => 'formerror' 
                        ), 
                    'label' => 'Title', 
                   )); 


        echo $this->Form->input('task_description_mm', array( 'type' => 'textarea', 
                     'cols'=>60, 
                     'rows' => 5, 
                     'div'=>'frm_filed_new', 
                     'error' => array( 'wrap' => 'div', 
                          'class' => 'formerror' 
                        ), 
                     'label' => 'Description', 
                   )); 

        echo $this->Form->input('task_from_mm',  array( 'div'=>'frm_filed_new','id'=>'task_from_mm','value'=>'', 
                    'error' => array( 'wrap' => 'div', 
                         'class' => 'formerror' 
                        ), 
                    'label' => 'From', 
                   )); 
        echo $this->Form->input('task_to_mm', array( 'div'=>'frm_filed_new','id'=>'task_to_mm','value'=>'', 
                    'error' => array( 'wrap' => 'div', 
                         'class' => 'formerror' 
                        ), 
                    'label' => 'To', 
                   )); 

       ?> 
       <br/> 
       <?php echo $this->Form->button('Submit', array('type'=>'submit','escape'=>true)); ?> 
      </fieldset> 
      <?php echo $this->Form->end(); ?> 

     </div> 

不工作的驗證。

我的代碼中有什麼錯誤? 我該如何解決這個問題?

編輯:

這是databse.php的配置錯誤的錯誤file.Now其改正。而print_r($errors)顯示errors.But,在視圖頁面不顯示,我的意思是文本框附近。

這是一個錯誤數組: Array ([task_title_mm] => Array ([0] => The title field is required) [task_description_mm] => Array ([0] => The description field is required) [task_from_mm] => Array ([0] => The from date field is required) [task_to_mm] => Array ([0] => The to date field is required))

我怎樣才能把它放在附近的文本框中?

回答

2

CakePHP旨在自動驗證模型並顯示驗證錯誤。自動驗證在模型保存上運行。在你的情況下:

$this->Task->save($this->request->data); 

上面會觸發驗證。沒有必要運行:$ this-> Task-> validates() - 如果你這樣做,你也必須照顧自己顯示驗證錯誤。所以我認爲你應該簡單地嘗試:

<?php 
class TasksController extends AppController { 
    var $name = 'Tasks'; 
    var $helpers = array('Html','Form','Session'); 

    function add_task() 
    { 

    if ($this->request->is('post')) { 
     // If the form data can be validated and saved... 
     if ($this->Task->save($this->request->data)) { 
     //saved and validated 
     } 
    } 
    } 
} 
?> 
+0

根據你的回答,如果我用你的代碼驗證和保存數據到數據庫表中會做什麼? – Kichu

+0

它不會保存,直到驗證 –

+0

我改變了控制器的代碼作爲你的,但沒有完成保存.... – Kichu

0

試試這個:

if ($this->Task->validates()) { 
      // it validated logic 
      //echo "ttt"; 
} else { 

    $this->validateErrors($this->Task); 
    $this->render(); 
} 
1

一件事,我在你的代碼注意到,你在你的模型

public var $validate=array(); 

正在寫,而不是嘗試

public $validate= array() or var $validate=array(); 

驗證應該w言辭之後。 謝謝:)