验证失败时,错误消息在一个模型上不起作用

时间:2012-03-23 15:00:00

标签: forms cakephp model validation cakephp-2.0

我有一个调用两个独立模型的表单。我的验证工作正确,错误输入的数据验证失败;但是,错误消息仅显示在RELATED模型数据上。以下是我的表格的两个模型片段:

echo $this->Form->input('Location.exchange', array('size'=>'3', 'error' => array('class' => 'error')));
echo $this->Form->input('Location.sln', array('size'=>'4', 'error' => array('class' => 'error')));
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('size'=>'65', 'label'=>'Your website\'s Specials page', 'error' => array('class' => 'error')));
echo '<br />';

只要在Location上验证失败,就会显示错误消息,而不是另一个(单位),这很讽刺,因为我在我的UnitsController中。这是控制器代码:

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

    if ($this->Unit->saveAll($this->request->data)) {            
        $this->Session->setFlash('Your property has been updated.', 'success');            
    } else {
        Set::merge($this->Unit->read(), $this->request->data);
    } 
}
}

以下是我的Location模型和Unit模型的验证数组的片段:   (来自模特单位):

public $validate=array(
    'type'=>array(
        'rule'=>'notEmpty',
        'message'=>'You must choose what type of property this is.'
    ),
    'unitnum'=>array(
        'rule'=>array('custom', '/^[a-z0-9 -\'.\/&]*$/i'),
        'message'=>'Must be the name or number of your unit.'
    )
 );

(来自模特位置):

public $validate = array(
        'area_code'=> array(
            'ac1'=> array(
                'rule'=>'numeric',
                'message'=>'Must be a number'
            ),
            'ac2'=>array(
                'rule'=>array('comparison', '>=',100),
                'message'=>'You must enter a valid area code'
            )
     );

1 个答案:

答案 0 :(得分:1)

如果你看一下Model-&gt; read()函数,你会发现它以

开头
$this->validationErrors = array();

所以行

Set::merge($this->Unit->read(), $this->request->data);

清除验证错误