I have noticed a problem when using checkboxes.
I load my form with default values like this:
//Defaults for this template
$default['video'] = '';
$default['height'] = '';
$default['width'] = '';
$default['codec'] = '';
$default['time'] = '0';
$default['audio'] = '';
$default['video_id'] = '';
$default['filename_machine'] = '';
//in this template we also use x pos and y pos, but these depend on screen layout ie. not stored on entity.
$data = (array)$data + (array)$default;
if ($data['audio']=='on') {
$audio = 'checked';
}
else {
$audio = '';
}
<td>
Spela upp ljud? <input type='checkbox' name='data_audio' $audio />
</td>
Now this works very well if the default is to not have a box checked, but if the default is to have a box checked, then that box will come up as checked even when the user has unchecked the box, saved the form and then gone back to edit the entity.
I think one way would be to not use checkboxes as the actual form field that gets saved, but use a hidden field behind, and only use the checkbox to set a value "on" or "off" in that field, or something like that. But a lot of extra work.
These specific form fields that I have defaults for in this way are saved by jQuery serialize so I don't have a good way of stepping through and manipulating fields based on type etc (like "if checkbox and not checked, set value to 'uncheck'" or something like that..), otherwise I guess that would be one way to go.
Is there a smarter trick I haven't thought of?
UPDATE
This is how I save all form fields prepended with data_
In the view I first do this before submission:
var myChunk = $( '#contentForm' ).serialize();
$( "#serialized" ).val( myChunk );
Then, in the php receiving the form:
//Get the custom data fields
$serialized = $this->unserializeForm( $request->request->get('serialized') );
$jsonArray = $this->getDataFields($serialized,true); //init the array that will become the json
$dataArray = $this->getDataFields($serialized); //init the array that will becom the one-dimensional array (currently in use)
Here is what the getDataFields look like, perhaps I could add some extra filtering in here for checkboxfields...
private function getDataFields($serialized,$toJson=false) {
$myArray = array();
foreach($serialized as $i) {
$label = $i[0];
$value = $i[1];
//find only the data fields (prepended with "data_", skip rest of form)
if(substr($label,0,5) == 'data_') {
//discard the "data_" part of the label
$label = substr($label,5);
if($toJson == true) {
//we're preparing data to be stored as json so we use a multidimensional for better overview.
//see if there is a subgroup to the label (ie. data_BACKGROUND_color or such)
if (strpos($label,'_') !== false) {
//echo 'har undergrupp <br />';
$parts = explode("_", $label); //split label
$group = $parts[0];
$label = $parts[1];
$myArray[$group][$label] = $value; //organize into group and label
}
else {
//echo 'har inte undergrupp <br />';
$myArray[$label] = $value;
}
}
else {
//we're storing data in a plain array
$myArray[$label] = $value;
}
}
}
return $myArray;
}