2010-09-26 90 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