I have a set of models that looks a bit like this
- Member
- Member_Addresses
- Agents
- AgentAddress
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');?>
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.
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 controlle开发者_Go百科r, 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.
How can I bubble up the validation error messages for MemberAddress and the agent models (Agent and AgentAddress)?
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
}
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.
精彩评论