我在Cakephp 2.0中对我的模型施加了一套验证规则,因为它们都是针对处理Web地址的字段,所以它们完全相同。剪切和粘贴准确。但是,当我在编辑表单中修改我的数据库时,只有第一个规则有效;所有其他人验证并更新我的记录。这是我的型号代码:
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.'
)
);
更新* 以下是我的表单:
<?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(); ?>
Cake是否有某种“你只能制定一次规则”的规则?或者我的模型字段名称实际上必须包含网站中的单词或其他内容?或者我刚刚做了一些愚蠢的语法错误?
更新这是我的控制器代码:
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 :(得分:1)
您要验证的网址是什么,您期望什么?除非您指定,否则url
验证不会检查协议。如果您想确保http
在网址中,您应该像这样指定您的规则:
'rule' => array('url', true)
请显示通过验证但无效的示例网址。
<强>更新强>
$this->data
应更改为$this->request->data
。这可能不会改变验证过程。但$this->data
已被弃用。
尝试在beforeValidate
模型中添加Unit
回调,并回复$this->data
以查看即将发生的事情。