Hey everybody,
I’m currently trying to render a checkboxlist with a individual layout. First of all, my data looks something like this on the database level: each row has an ID, a genre and a subgenre (it’s about music). The very first entry in the subgenre field for each genre is the genre itself… so it’s something like (1, Rock, Rock) (2, Rock, Alternative) and so on.
Now I wanna render a checkboxlist in which the first entry is on it’s own line (because it’s the overall genre) and all the subgenres on a new line, so ultimately something like this
[ ] Rock
----- [ ] Alternative [ ] Grunge
[ ] Electro
----- [ ] EDM [ ] House
I hope you get it… My current approach is to get an array such as [[Rock] => [Rock, Alternative, Grunge], [Electro] => [Electro, EDM, House] from my database with
$this->musicSubgenres = \yii\helpers\ArrayHelper::map(\app\models\Music::find()->all(), 'id', 'subgenre', 'genre');
After that, I render a separate checkboxlist, depending if it’s the first item in the values in the list above (the array above is saved in $model->musicSubgenres):
foreach($model->musicSubgenres as $genre) {
// generate arrays for values and keys
$subgenreValues = array_values($genre);
$subgenreKeys = array_keys($genre);
// echo first element as genre
echo $form->field($model, 'music')->checkBoxList([$subgenreKeys[0] => $subgenreValues[0]])->label(false);
// unset first element
unset($subgenreKeys[0]);
unset($subgenreValues[0]);
// echo the rest as subgenres
if(($count = count($subgenreValues)) > 0) {
$result = [];
for($i = 1; $i <= $count; $i++)
$result[$subgenreKeys[$i]] = $subgenreValues[$i];
echo $form->field($model, 'music')->checkBoxList($result, ['itemOptions' => ['uncheck' => false]])->label(false);
}
}
The main problem is, that each checkboxlist gets its own hidden input field so that all the values from the previous checkboxes are not in my $_POST. Anybody knows a better solution or how to disable the rendering of the hidden inputs? I also already tried solving it with the per ‘item’ => function(), but this generates the problem of having the right boxes checked on update.
Thanks in advance