2012-03-03 59 views
0

我有一套驗證規則,我在Cakephp 2.0中對我的模型施加了限制,並且因爲它們都是用於處理網址的字段,所以它們完全相同。切割和粘貼確切。但是,當我在編輯窗體中修改數據庫時,只有第一條規則有效;所有其他人都會驗證並更新我的記錄。這裏是我的模型代碼:只有第一個驗證規則工作

public $validate=array(
    'unit_website'=>array(
     'rule'=>'url', 
     'message'=>'You must enter a valid website address.' 
    ), 
    'rates'=>array(
     'rule'=>'url', 
     'message'=>'You must enter a valid website address.' 
    ), 
    'book'=>array(
     'rule'=>'url', 
     'message'=>'You must enter a valid website address.' 
    ), 
    'contact'=>array(
     'rule'=>'url', 
     'message'=>'You must enter a valid website address.' 
    ) 
); 

UPDATE *這裏是我的形式:

<?php  
echo $this->Form->create('Unit', array(
    'action' => 'edit', 
    'inputDefaults' => array(
     'label' => false, 
     'div' => false 
     ) 
    ) 
); 
echo $this->Form->hidden('id'); 
echo $this->Form->hidden('location_id'); 
echo $this->Form->hidden('Location.id'); 
echo $this->Form->hidden('Location.lat'); 
echo $this->Form->hidden('Location.lon'); 
echo $this->Form->input('Location.prop_name', array('label'=>'Property name where your unit is located', 'div'=>true)); 
if($this->data['Unit']['type']=='condo' || $this->data['Unit']['type']=='house') { 
echo $this->Form->input('Unit.unitnum', array('label' => 'Unit\'s Name/Number')); 
} 
echo $this->Form->input('type', array('label'=>'Type of Property', 'onChange'=>'toggle()', 'options'=>array('condo'=>'Condo', 'house'=>'House', 'hotel'=>'Hotel or Motel', 'rentalco'=>'This is a rental company'))); 
echo $this->Form->input('Location.address', array('onChange'=>'getLatLong(address)', 'size'=>'50', 'div'=>true)); 
echo $this->Form->input('Location.city', array('onChange'=>'getLatLong(address)')); 
echo $this->Form->input('Location.state', array('onChange'=>'getLatLong(address)', 'size'=>'2')); 
echo $this->Form->input('Location.zip', array('size'=>'5')); 
echo '<br />'; 
echo $this->Form->input('Location.area_code', array('size'=>'3')); 
echo $this->Form->input('Location.exchange', array('size'=>'3')); 
echo $this->Form->input('Location.sln', array('size'=>'4')); 
echo '<br />'; 
echo $this->Form->input('unit_website', array('size'=>'65', 'label'=>'Your unit\'s website', 'error' => array('class' => 'error'))); 
echo '<br />'; 
echo $this->Form->input('specials', array('label' => 'Your website\'s Specials page', 'size'=>'65', 'error' => array('class' => 'error'))); 
echo '<br />'; 
echo $this->Form->input('rates', array('size'=>'65', 'error' => array('class' => 'error'), 'label'=>'Your unit\'s rates page')); 
echo '<br />'; 
echo $this->Form->input('book', array('size'=>'65', 'error' => array('class' => 'error'), 'label'=>'Your unit\'s booking page or calendar')); 
echo '<br />'; 
echo $this->Form->input('contact', array('size'=>'65', 'error' => array('class' => 'error'), 'label'=>'Your unit\'s contact or email page')); 
echo '<br />'; 
if($this->data['Unit']['type']=='condo' || $this->data['Unit']['type']=='house') { 
echo '<br /><div id="unit_info">'; 
echo $this->Form->input('sq_ft', array('label'=>'Square feet', 'min'=>'1000', 'max'=>'10000', 'step'=>'50')); 
echo $this->Form->input('bedrooms', array('label'=>'Bedrooms', 'min'=>'1', 'max'=>'15', 'step'=>'1')); 
echo $this->Form->input('baths', array('label'=>'Bathrooms', 'min'=>'1', 'max'=>'10', 'step'=>'.5')); 
echo $this->Form->input('sleeps', array('label'=>'How many people does your unit sleep?', 'min'=>'1', 'max'=>'45', 'step'=>'1')); 
echo '</div><br />'; 
echo $this->Form->input('unit_desc', array('div'=>true, 'cols'=>'150', 'rows'=>'4', 'label'=>'Describe your unit <span style="font-size:14px; font-style:italic">(Remember to include the sizes of beds in each bedroom, whether you have a sleeper sofa, deluxe kitchens and baths, balconies and their views, access to amenities, and any other items that distinguish your property.)</span>')); 
} 
else { 
echo '<br />'; 
echo $this->Form->input('unit_desc', array('div'=>true, 'cols'=>'150', 'rows'=>'4', 'label'=>'Describe your hotel or rental company')); 
} 
echo '<br />&nbsp;'; 
echo $this->Form->end('Update Property');?> 
<?php echo $this->Session->flash(); ?> 

蛋糕是否有某種「只能使這條規則一次」的規則?或者,我的模型字段名稱是否必須在其中包含網站或其他內容?或者我只是犯了一些愚蠢的語法錯誤?

UPDATE這裏是我的控制器代碼:

function edit($id) { 
$this->set('title', 'Edit your property'); 
$this->Unit->id = $id;  
if (empty($this->data)) {   
$this->data = $this->Unit->read();  
} else { 

    if ($this->Unit->saveAll($this->data)) {    
     $this->Session->setFlash('Your property has been updated.', 'success');    

     } 
} 
} 
+0

你會顯示控制器,然後整個表單?例如,我希望在視圖中看到您的form-> create()調用。我也想看看處理編輯的控制器方法。 – 2012-03-05 23:59:07

+0

當然,添加到OP。 – huzzah 2012-03-06 15:05:11

回答

1

什麼是您要驗證的URL,你有什麼期待?除非您指定它,否則url驗證不會檢查協議。如果你想確保http在URL中,你應該指定你的規則是這樣的:

'rule' => array('url', true) 

請表明逝去的驗證,但不是有效的樣本網址。

UPDATE

$this->data應改爲$this->request->data。這可能不會改變驗證過程。但$this->data已棄用。

嘗試在Unit模型中添加一個beforeValidate回調並回顯$this->data以查看接下來會發生什麼。

+0

像1234和$ thisisnotddata。這些通過驗證並進入數據庫。但是,如果我嘗試在'unit_website'(數組中的第一條規則和url的第一個表單域)中輸入1234或$ notdata或其他任何內容,我會收到錯誤消息並且不會進行驗證。我不希望這些字段中有任何特定的url,只有任何有效的url。 – huzzah 2012-03-05 15:41:02

+0

因此,出於某種原因(我發誓自從我們發言以來我沒有觸及過這段代碼!),除了其中一條驗證規則之外,其他所有條件都可以正常工作,並獲取此...現在錯誤消息不再顯示......任何表單元素爲模型單位。模型位置的所有表單元素...錯誤消息通常在驗證失敗時顯示。我認爲我的Cake副本只是鬧鬼,就這些。 – huzzah 2012-03-22 20:51:43