2012-03-23 68 views
1

我有一个表单调用两个单独的模型。我的验证工作正确,输入错误的数据验证失败;但是,错误消息仅显示在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 />'; 

将错误消息显示,只要在验证失败的位置,而不是其他的(这是单位),这是具有讽刺意味的,因为我在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); 
    } 
} 
} 

,这里是从我的两个位置模型验证阵列和我的单位模型的一个片段: (从模型单位):

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

如果你看一下型号 - >阅读()函数,你会看到它与

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

所以行

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

清除验证错误

+0

嘿。从来没有想过要查看read()函数的实际定义。好吧,现在我明白了;我的问题是没有merge()方法,我不保存我的整个数据数组,供用户更正错误并正确更新记录。有关解决此问题的任何建议? – huzzah 2012-03-23 15:39:04

+0

如果不修改'$ this-> request-> data'的内容,那么当验证失败时,从表单提交的所有内容都应该再次显示在表单中。无论如何,我想'find()'而不是'read()'应该保留验证错误。 – nIcO 2012-03-23 15:46:01

+0

嗯。我一直坚持使用merge()函数,因为我实际上是从表单中没有使用的模型中丢失所有数组数据,如http://stackoverflow.com/questions/9690300/cakephp-这个数据,失去一批量-个,其数据阵列后的形式提交。它之前一直在引发一个问题,但我已经改变了我的编辑页面,所以这不再是问题。所以我可以删除这条线。所以谢谢! – huzzah 2012-03-23 16:09:10

相关问题