2011-02-02 45 views
0

我一直在這一天的大部分時間,我不能得到這個爲我的生活工作(以及我可以得到它1/2工作但不完全正確)。

基本上,我試圖用驗證在搜索表單域,像這樣:

if(isset($search['ApplicantAge']) && !empty($search['ApplicantAge'])) { 
     if ($this->Plan->validates()) { 
     $ApplicantAge = $search['ApplicantAge']; 
     } 
    } 

這裏是我的模型代碼:

...

'ApplicantAge' => array(
'required' => true, 
'allowEmpty' => false, 
'rule' => 'numeric', 
'message' => 'A valid Age is required. Please enter a valid Age.'), 

.. 。

驗證正在工作,但是當我輸入一個數字(數字)時,它顯示我的錯誤!而當它是空白NO錯誤顯示,當我輸入字母似乎工作:?(??

有誰知道一招,這一奇怪的行爲

+3

`isset($ search ['ApplicantAge'])&&!empty($ search ['ApplicantAge'])`是多餘的。只要`!空($ search ['ApplicantAge'])`就可以。 – deceze 2011-02-03 01:25:08

回答

1

嘗試使用一個「NotEmpty」的規則,而不是需要的/ allowEmpty東西。

'ApplicantAge' => array(
'applicant-age-numeric'=> array(
    'rule' => 'numeric', 
    'message' => 'A valid Age is required. Please enter a valid Age.' 
    ), 
'applicant-age-not-empty'=> array(
    'rule' => 'notEmpty', 
    'message' => 'This field cannot be left blank' 
    ) 
) 
0

該手冊並沒有幫助我的人。(

但你對驗證=>僅陣列建議似乎已經完成了招我這是怎麼得到它的工作:

plans_controller。 PHP

​​

plan.php(模型)

var $validate = array(
    'ApplicantAge' => array(
     'applicant-age-numeric' => array(
      'rule' => 'numeric', 
      'message' => 'A valid Age is required. Please enter a valid Age.'), 
     'applicant-age-not-empty' => array(
      'rule' => 'notEmpty', 
      'message' => 'This field cannot be left blank'), 
    ), 

現在,如果在ApplicateAge字段中沒有輸入數據,則會顯示正確的消息。如果輸入了非數字,則還會顯示正確的消息。

這比我想象的要難得多!

0

爲了記錄,我將對我早期接受的帖子進行更正。沒有人知道validate =>只有在save()實際上仍然將數據保存到我的計劃表中。

我能夠使用set()獲得它的工作。下面是徹底解決該問題的代碼:

plans_controller.php

if (isset($search['ApplicantAge'])) { 
     $this->Plan->set($this->data); 
     if ($this->Plan->validates()) { 
      $ApplicantAge = $search['ApplicantAge']; 
     } 
    } 

計劃。php(model):

var $validate = array(
    'ApplicantAge' => array(
     'applicant-age-numeric' => array(
      'rule' => 'numeric', 
      'message' => 'A valid Age is required. Please enter a valid Age.'), 
     'applicant-age-not-empty' => array(
      'rule' => 'notEmpty', 
      'message' => 'This field cannot be left blank'), 
    )