Reputation: 6986
I'm developing a site using the CakePHP framework. I have a form which shows a list of entities, with a tickbox for each one allowing you to select it. You then press a button at the bottom of the form saying "Edit selected".
My usual approach is to give each checkbox input the same name (e.g. row_id
) and use the primary ID as the input's value
. However, when you submit the form, CakePHP only seems to return one of the checkbox's values to the controller in $this->params['url']
, rather than any kind of list like I'd expect.
Any tips on the right way to handle this, so that I can find out which rows were ticked?
Upvotes: 0
Views: 1172
Reputation: 6721
If you're using the FormHelper, the easiest way is to create your fields like this:
echo $form->input('ModelName.0.row_id', /* snip */)
echo $form->input('ModelName.1.row_id', /* snip */)
Etc.. you get the point. If it's a dynamic list, there always the for
loop.
When the form is POSTed, this should give you a server side array like this ($this->data):
array
(
['ModelName'] => array
(
[0] => array
(
[row_id] => value
),
[1] => array
(
[row_id] => value
)
)
)
Then you can use the Set utility class to extract your IDs (and mess with your data:))
Upvotes: 1