2014-03-06 90 views
0

再次有很多類似的問題,但沒有一個真正幫助我。 HTML5表單驗證似乎與消息來觸發它應該是「請輸入模式」CakePHP - 模型驗證不起作用

我有一個表格將計算機添加到數據庫中的模型驗證消息的代替「請填寫此字段」。

這裏是我的形式:

echo $this->Form->create('Computer'); 
echo $this->Form->input('Computer.model', array('label' => 'Model')); 
echo $this->Form->input('Computer.memory', array('label' => 'memory')); 
echo $this->Form->input('Computer.hdd', array('label' => 'hdd')); 
echo $this->Form->input('Computer.price', array('label' => 'price'));  
echo $this->Form->end('Save Computer'); 

這裏是指數全面控制代碼,並添加行爲

<?php 
class ComputersController extends AppController { 

    public $helpers = array('Html', 'Form', 'Session'); 
    public $components = array('Session'); 


    public function beforeFilter() { 
     parent::beforeFilter(); 
     $this->Auth->allow('add'); 
    } 
    public function index() { 


     $this->set('computers', $this->Computer->find('all')); 

    } 

    public function add() { 

     if ($this->request->is('post')) { 
      if (!empty($this->request->data)) { 

       $this->Computer->save($this->request->data); 
       $this->Session->setFlash(__('Your Computer has been saved, or so it seems.....')); 
       return $this->redirect(array('action' => 'index')); 
      } 
      $this->Session->setFlash(__('Not sure why we got here 1.')); 
     } else { 
      $this->Session->setFlash(__('By right, this should be the index page')); 


     } 

    } 

} 
?> 

這裏的模型

<?php 
class Computer extends AppModel { 


public $validate = array(

    'model' => array(
     'Please enter model name'=> array(
      'rule'=>'notEmpty', 
      'message'=>'Please enter model' 
     ) 
    ) 

); 
} 

?> 

我與其他形式的閱讀觸發模型保存功能,我會自動觸發模型驗證。我怎樣才能讓模型驗證工作?

感謝 凱文

+0

注:模型字段默認爲<輸入名稱= 「數據[計算機] [模型]」 最大長度= 「10」 類型= 「文本」 ID =「ComputerModel 「required =」required「> 我發現一些疑難解答,它是」需要「,因爲有一個模型驗證規則'notEmpty'。這很棒。但是,它仍然不顯示模型驗證消息,而是顯示HTML5消息。 – aDvo

+2

Cake只添加'required =「required」',這是由瀏覽器處理的。如果您'檢查元素'並手動刪除'必需'屬性,這將到達服務器,您將從該模型中獲得消息。 Cake不會更改瀏覽器驗證消息。這可以手動更改(http://stackoverflow.com/questions/5272433/html5-form-required-attribute-set-custom-validation-message) – cornelb

+0

類似於這個http://stackoverflow.com/a/21094082/ 2776508 – arilia

回答

0

$this->{Model}->save()返回false如果驗證失敗,但在你的情況你用閃光燈消息重定向保存功能後。所以首先檢查表格是否完全保存,如果完美保存,然後重定向到列表頁面,則可以使用閃存消息顯示view文件,您可以在其中查看驗證消息。

if ($this->Computer->save($this->request->data)) { 
    $this->Session->setFlash(__('Your Computer has been saved, or so it seems.....')); 
    return $this->redirect(array('action' => 'index')); 
} else { 
    $this->Session->setFlash(__('Unable to save form')); 
} 

注:要禁用HTML驗證只是做

$this->Form->inputDefaults(array(
    'required' => false 
)); 

在你的視圖文件

希望這有助於你。

+0

謝謝,這是我正在尋找的答案。其他答案仍然有幫助。調用模型保存方法失敗並重定向到索引頁會導致顯示模型錯誤消息並不明顯。它只是不明顯.... – aDvo

1

正如您所說,如果您在模型中具有notEmpty驗證,CakePHP會在輸入屬性上添加required="required"。這是由瀏覽器處理的,所以當您嘗試提交空值時,您會看到默認的Please enter this field消息。一個好處是,如果您使用不同語言的瀏覽器,則該消息將以該語言顯示。

如果您想更改該消息,您可以嘗試類似this question的解決方案。 (這可能不是你想要的)

如果你想刪除客戶端的消息,您可以使用novalidate

echo $this->Form->create('Computer', array('novalidate' => 'novalidate')); 

這種方式禁用它,在HTML5所需的屬性將被忽略,而你將從模型中獲取消息。

我不確定是否有辦法告訴Cake在客戶端上使用服務器端的值。

+0

不知道有什麼方法可以告訴Cake在客戶端使用服務器端的值。意思是(通過ajax驗證嗎?不,沒有)。 +1。 – AD7six