【问题标题】:Only first validation rule working只有第一个验证规则有效
【发布时间】:2012-03-03 01:02:46
【问题描述】:

我在 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(); ?>

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');            

        }   
}
}

【问题讨论】:

  • 您会先显示控制器,然后再显示整个表单吗?例如,我想在视图中查看您的 form->create() 调用。我还想看看处理编辑的控制器方法。

标签: validation cakephp-2.0


【解决方案1】:

您正在验证的 URL 是什么,您期望什么? url 验证不会检查协议,除非您指定它。如果你想确保http 在 url 中,你应该像这样指定你的规则:

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

请显示通过验证但无效的示例 URL。

更新

$this-&gt;data 应更改为 $this-&gt;request-&gt;data。这可能不会改变验证过程。但是$this-&gt;data 已被弃用。

尝试在Unit 模型中添加beforeValidate 回调并回显$this-&gt;data 以查看发生了什么。

【讨论】:

  • 诸如 1234 和 $thisisnotddata 之类的东西。这些正在通过验证并进入数据库。但是,如果我尝试在“unit_website”(数组中的第一条规则和 url 的第一个表单字段)中输入 1234 或 $notdata 或其他任何内容,我会收到错误消息并且它不会验证。我不希望这些字段中有任何特定的 url,只要任何有效的 url。
  • 因此出于某种原因(我发誓自从我们交谈后我就没有碰过这段代码!),除了一个验证规则之外的所有验证规则都有效,并且得到这个......现在错误消息不再显示...在模型单元的任何表单元素上。模型位置的所有表单元素...验证失败时错误消息正常显示。我认为我的 Cake 副本只是闹鬼,仅此而已。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-02-03
  • 1970-01-01
  • 2021-11-23
  • 2016-05-13
  • 2016-09-24
  • 2011-10-25
  • 2020-03-02
相关资源
最近更新 更多