验证消息不显示在CakePHP中的相关模型 [英] Validation Messages Don't Show for Related Models in CakePHP

查看:106
本文介绍了验证消息不显示在CakePHP中的相关模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a set of models that looks a bit like this


  • 会员

    • Member_Addresses

    • 代理

      • AgentAddress

      我也有一个表单试图让你更新所有这些不同的模型在一种形式。我对这种在CakePHP中创建表单的方式有些新鲜,我无法获取验证错误消息以我的形式显示。

      I also have a form that tries to let you update all these different models in one form. I am bit new to this way of making forms in CakePHP, and I'm having trouble getting the validation error messages to show up in my form.

      我的表单看起来像这样:

      My form looks something like this:

      <?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');?>
      

      此表单的要点是一个用户配置文件),以及代理的联系信息...代理模型为Agent和AgentAddress。我正在为MemberAddresss使用hasMany关系,但只允许用户提供两个地址。

      The gist of this form is a user-profile (Member), with two slots for addresses (MemberAddress), along with contact info for an "agent"...the agent models being Agent and AgentAddress. I'm using a hasMany relationship for the MemberAddresses but only allowing users to provide two addresses.

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

      I am getting the validation messages for the top-level Member model, but I am not getting validation messages for the related models. In my controller, I decided to not use saveAll() because with the MemberAddress.0 and MemberAddress.1 items, I wouldn't be able to save anything when leaving the 2nd MemberAddress (MemberAddress.1) blank. So I changed saveAll() to save, pushed the saving logic into the MemberAddress model, and I return a boolean value from my MemberAddress->update() call in the model to signal success or failure.

      如何为MemberAddress和代理模型(Agent和AgentAddress)提供验证错误消息?

      How can I bubble up the validation error messages for MemberAddress and the agent models (Agent and AgentAddress)?

      推荐答案

      如果您不想使用saveAll,您只能用于验证目的,例如:

      if you don't want to use saveAll you can use it only for validation purposes i.e.:

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

      验证的问题是,错误需要附加到正确的表单元素,自定义保存函数,当它们需要附加到MemberAddress.0.city时,错误附加到MemberAddress.city。

      the problem with the validation is that the errors need to be attached to the proper form elements, and while you are using some custom save function, the errors are attached to MemberAddress.city while they need to be attached to MemberAddress.0.city.

      这篇关于验证消息不显示在CakePHP中的相关模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆