代码之家  ›  专栏  ›  技术社区  ›  Randy L

CakePHP中不会显示相关模型的验证消息

  •  0
  • Randy L  · 技术社区  · 15 年前

    我有一套模型看起来有点像这样

    • 成员
      • 会员地址
      • 代理人

    我还有一个表单,它试图让您在一个表单中更新所有这些不同的模型。我对用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');?>
    

    这个表单的要点是一个用户配置文件(Member),有两个地址槽(MemberAddress),还有一个“agent”的联系信息……agent模型是agent和AgentAddress。我对MemberAddresses使用hasMany关系,但只允许用户提供两个地址。

    我正在获取顶级成员模型的验证消息,但没有获取相关模型的验证消息。在我的控制器中,我决定不使用saveAll(),因为对于MemberAddress.0和MemberAddress.1项,当第二个MemberAddress(MemberAddress.1)为空时,我将无法保存任何内容。因此,我将saveAll()更改为save,将保存逻辑推入MemberAddress模型,然后从模型中的MemberAddress->update()调用返回一个布尔值,以表示成功或失败。

    如何为MemberAddress和代理模型(agent和AgentAddress)生成验证错误消息?

    1 回复  |  直到 15 年前
        1
  •  2
  •   Nik Chankov    15 年前

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