CakePHP - save multiple HABTM data w/ dropdowns in same form -
when user adds event, want them able choose band(s) playing @ event. have events table , bands table each habtm model associations other.
on "add event" page, have dropdown displaying bands, can choose one.
echo $this->form->input('band', array('multiple'=>false, 'empty'=>true));
i have "add band" button, , when clicked, adds dropdown. think know how dynamic field-adding thing - - when try this: (just see if can work)
echo $this->form->input('band', array('multiple'=>false, 'empty'=>true)); echo $this->form->input('band', array('multiple'=>false, 'empty'=>true)); echo $this->form->input('band', array('multiple'=>false, 'empty'=>true));
it doesn't save 3 rows in bands_events habtm table - saves one. , when try edit event, 3 select dropdowns default-select 1 of selected bands, not (obviously can't, since didn't save data).
any thoughts on how can have multiple dropdowns add more 1 band event? there way many bands have checkboxes - , hate multi-select boxes - difficult users.
any appreciated.
you need use different name attributes each band input , set structure correctly saving habtm relationship. assuming you've pushed band list $bands.
echo $this->form->input('band.band.0', array('multiple'=>false, 'empty'=>true, 'options'=>$bands)); echo $this->form->input('band.band.1', array('multiple'=>false, 'empty'=>true, 'options'=>$bands)); echo $this->form->input('band.band.2', array('multiple'=>false, 'empty'=>true, 'options'=>$bands));
this should produce correct structure when user submits form in $this->data[band][band] array of band ids.
when load existing event editing, need iterate through bands , explicitly set default value:
foreach($this->data['band']['band'] $index => $band_id) { echo $this->form->input("band.band.$index", array('multiple'=>false, 'empty'=>true, 'options'=>$bands, 'value'=>$band_id))); }
Comments
Post a Comment