チェックボックスの使用時に問題があることに気付きました。
次のようなデフォルト値でフォームをロードします。
//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>
デフォルトでボックスがチェックされていない場合、これは非常にうまく機能しますが、デフォルトでボックスがチェックされている場合、ユーザーがボックスのチェックを外し、フォームを保存してから行った場合でも、そのボックスはチェックされた状態で表示されますエンティティの編集に戻ります。
1つの方法は、保存される実際のフォームフィールドとしてチェックボックスを使用せず、背後にある非表示のフィールドを使用し、チェックボックスのみを使用してそのフィールドに値「オン」または「オフ」を設定すること、またはそのようなものだと思います. しかし、余分な作業がたくさんあります。
このようにデフォルトを設定しているこれらの特定のフォーム フィールドは jQuery シリアル化によって保存されるため、タイプなどに基づいてフィールドをステップスルーして操作する良い方法がありません」またはそのようなもの..)、そうでなければ、それが1つの方法だと思います.
私が考えていなかったより賢いトリックはありますか?
アップデート
これは、data_ を前に付けてすべてのフォーム フィールドを保存する方法です。
ビューでは、送信前に最初にこれを行います。
var myChunk = $( '#contentForm' ).serialize();
$( "#serialized" ).val( myChunk );
次に、フォームを受け取るphpで:
//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)
これが getDataFields の外観です。おそらく、ここにチェックボックスフィールド用のフィルタリングを追加できます...
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;
}