I have a Unit which hasMany Images, and I'm trying to display an edit form for these images on my edit page. My resulting html form input shows the wrong model, so the correct value for this data doesn't get populated. Here is my view code:
<?php
echo $this->Form->create('Unit', array(
'action' => 'edit',
'inputDefaults' => array(
'label' => false,
'div' => false
)
)
);
echo $this->Form->hidden('id');
foreach($this->data['Image'] as $img) {
echo $this->Form->hidden('Image'.$img['id'].'id');
}
echo $this->Form->end('Update Photo');?>
<?php echo $this->Session->flash(); ?>
My html ends up like this:
<form action="/units/edit/3" id="UnitEditForm" method="post" accept-charset="utf-8">
<div style="display:none;">
<input type="hidden" name="_method" value="POST"/>
</div>
<input type="hidden" name="data[Unit][id]" value="3" id="UnitId"/>
<input type="hidden" name="data[Unit][Image1id]" id="UnitImage1id"/>
<input type="hidden" name="data[Unit][Image2id]" id="UnitImage2id"/>
<input type="hidden" name="data[Unit][Image3id]" id="UnitImage3id"/>
<input type="hidden" name="data[Unit][Image4id]" id="UnitImage4id"/>
<input type="hidden" name="data[Unit][Image5id]" id="UnitImage5id"/>
<input type="hidden" name="data[Unit][Image6id]" id="UnitImage6id"/>
<div class="submit">
<input type="submit" value="Update Photo"/>
</div>
</form>
I have seen a number of tutorials, including the CakePHP manual which shows how you can enter 'Modelname.[0].field', etc, which is all fine and good for when you know how many rows you have, but when does that ever happen in a CMS? I cannot seem to find any information on how to build this form by looping through all the records in the array of Images available. Eventually, I want to be able to have the hidden id field so the record can be updated, show the actual image (and the ability to change it), it's alt description (and the ability to change it), and I suppose another hidden field for the unit_id that associates it with this form.
UPDATE using
echo $this->Form->hidden('Image.'.$img['id'].'.id');
works now, but is returning this:
<input type="hidden" name="data[Image][1][id]" value="2" id="Image1Id"/>
<input type="hidden" name="data[Image][2][id]" value="3" id="Image2Id"/>
<input type="hidden" name="data[Image][3][id]" value="4" id="Image3Id"/>
<input type="hidden" name="data[Image][4][id]" value="5" id="Image4Id"/>
<input type="hidden" name="data[Image][5][id]" value="6" id="Image5Id"/>
<input type="hidden" name="data[Image][6][id]" id="Image6Id"/>
I need to start from [0], not [1]. Instead of getting the number by 'id', I need to be getting the counted numbers in the $img array, correct? How do I do this?