I have a checkbox in a Cakephp form that can have multiple values. In the view:
<?php // Multiple checkbox form
echo $this->Form->input('report_types', array(
'type'=>'select',
'label'=>'Report Type(s)',
'multiple'=>'checkbox',
'options'=>array(
'option 1'=>'option 1',
'option 2'=>'option 2',
'option 3'=>'option 3',
),
)); ?>
When I load this into the database it returns a "Column not found: 1054 Unknown column 'Array' in 'field list'" error because it is trying to add an array where it should be a string.
I've tried converting any of the $this->request->data
that is in array form into a string, but that interferes with the date selects that I have earlier in the form (they are stored as arrays as well).
I've also tried converting just the value of the multiple check-box field into a string in the beforeValidate()
method in the model, but it requires too much repetition and gets messy when I need to 'unpack' the data:
<?php class Request extends AppModel {
...
function beforeValidate() {
if(!empty($this->request->data['Request']['attachment_types'])) {
$this->data['Request']['attachment_types'] = implode(',', $this->data['Request']['attachment_types']);
}
if(!empty($this->request->data['Request']['report_types'])) {
$this->data['Request']['report_types'] = implode(',', $this->data['Request']['attachment_types']);
}
// Am I going to have to keep adding if-statements here?
}?>
Besides, the !empty()
method doesn't work because the field will never be empty (due to the hidden field automatically created when CakePHP makes a checkbox input).
Does anyone have any ideas as to how I can submit a multiple check-box input into a database? It seems like a pretty modest request...does CakePHP have any "automagic" skills in this respect?