我有一套驗證規則,我在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 /> ';
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');
}
}
}
你會顯示控制器,然後整個表單?例如,我希望在視圖中看到您的form-> create()調用。我也想看看處理編輯的控制器方法。 – 2012-03-05 23:59:07
當然,添加到OP。 – huzzah 2012-03-06 15:05:11