2010-09-26 87 views
0

我有一组模型,看起来有点像这样验证消息不显示的相关型号CakePHP中

  • 会员
    • Member_Addresses
    • 代理
      • AgentAddress

我也有一种形式,试图让您在一种形式中更新所有这些不同的模型。我对CakePHP中的这种表单制作方式有点新,而且我很难将验证错误信息显示在我的表单中。

我的形式看起来是这样的:与联系信息

<?php echo $this->Form->create('Member',array('type' => 'file'));?> 
<fieldset> 
<?php 
    echo $this->Form->input('first_name'); 
    echo $this->Form->input('last_name'); 
?> 
</fieldset> 
<fieldset> 
    <? 
    echo $this->Form->input('MemberAddress.0.line_1'); 
    echo $this->Form->input('MemberAddress.0.city'); 
    echo $this->Form->input('MemberAddress.0.state'); 
    echo $this->Form->input('MemberAddress.0.zip'); 
    ?> 
</fieldset> 
<fieldset> 
<? 
    echo $this->Form->input('MemberAddress.1.line_1'); 
    echo $this->Form->input('MemberAddress.1.city'); 
    echo $this->Form->input('MemberAddress.1.state'); 
    echo $this->Form->input('MemberAddress.1.zip'); 
?> 
</fieldset> 
<fieldset> 
    <? 
    echo $this->Form->input('Agent.0.agent',array('type'=>'text')); 
    echo $this->Form->input('Agent.0.agency'); 
    echo $this->Form->input('Agent.0.email'); 
    echo $this->Form->input('Agent.0.phone'); 
    ?> 
</fieldset> 
<fieldset> 
<? 
    echo $this->Form->input('Agent.0.AgentAddress.line_1'); 
    echo $this->Form->input('Agent.0.AgentAddress.city'); 
    echo $this->Form->input('Agent.0.AgentAddress.state'); 
    echo $this->Form->input('Agent.0.AgentAddress.zip'); 
?> 
</fieldset> 
<?php echo $this->Form->end('Submit');?> 

这种形式的要点是用户配置文件(会员),有两个插槽地址(MemberAddress),以及一个“代理” ...代理模型是Agent和AgentAddress。我为MemberAddresses使用hasMany关系,但只允许用户提供两个地址。

我得到顶级成员模型的验证消息,但我没有得到相关模型的验证消息。在我的控制器中,我决定不使用saveAll(),因为使用MemberAddress.0和MemberAddress.1项目时,当将第二个MemberAddress(MemberAddress.1)留空时,我将无法保存任何内容。所以我改变了saveAll()来保存,将保存逻辑推入到MemberAddress模型中,并且从模型中的MemberAddress-> update()调用中返回一个布尔值来表示成功或失败。

我怎么能泡涨了MemberAddress验证错误消息和代理模型(Agent和AgentAddress)?

+0

模型的验证规则应该位于各个模型本身中,每个模型都插入或更新它。 – 2010-09-26 10:07:55

回答

2

,如果你不想用白水你可以使用它只是为了进行验证,即:

if($this->Member->saveAll($this->data, array('validate'=>'only'))){ 
    //your custom save function 
} 

与验证的问题是,需要连接到正确的表单元素的错误,当您使用一些自定义保存功能时,这些错误将附加到MemberAddress.city,而它们需要连接到MemberAddress.0.city。

+0

好的,这是我认为可能是问题。我现在再看看它。谢谢。 – the0ther 2010-09-26 20:44:14

+0

好多了!再次感谢。 – the0ther 2010-09-26 21:20:04