Checked items in Yii2 checkboxList
<p>I've got a dummy problem with checkboxList in ActiveForm.</p>

<p>So I've got two models (I cut off irrelevant code):</p>

<p>Object model:</p>

<pre><code>class Object extends ActiveRecord
{
    public $oFacilities = array();

    public static function tableName()
    {
        return 'objects';
    }

    public function getObjectFacilities(){
        return $this-&gt;hasMany(Facility::className(),['id' =&gt; 'facilityId'])-&gt;viaTable('objectsfacilities', ['objectId' =&gt; 'id']);
    }
}
</code></pre>

<p>Facility model:</p>

<pre><code>class Facility extends ActiveRecord
{
    public static function tableName()
    {
        return 'facilities';
    }

    public static function forWidget(){
        $facilities =static::find()-&gt;select(['id','name'])-&gt;where(['type' =&gt; static::TYPE_OBJECT])
                                   -&gt;orderBy('name')
                                   -&gt;asArray()
                                   -&gt;all();

        return ArrayHelper::map($facilities, 'id', 'name');
    }

    public function getObjectsWithFacility(){
        return $this-&gt;hasMany(Object::className(), ['id' =&gt; 'objectId'])-&gt;viaTable('objectsfacilities', ['facilityId' =&gt; 'id']);
    }
}
</code></pre>

<p>And I've got two actions in my ObjectController:</p>

<pre><code>class ObjectController extends Controller
{
    public function actionCreate(){
        $model = new Object();
        if($model-&gt;load(Yii::$app-&gt;request-&gt;post()) &amp;&amp; $model-&gt;validate()){
            $model-&gt;save();
            foreach($model-&gt;oFacilities as $id){
                $facility = Facility::findOne($id);
                $model-&gt;link('objectFacilities', $facility);
            }
            Yii::$app-&gt;session-&gt;setFlash('alert', 'object-created');
            return $this-&gt;redirect('index');
        }
        return $this-&gt;render('create', ['model' =&gt; $model]);
    }

    public function actionUpdate($id){
    /* This action is complete - for now I just want to render a form */
        if($model = Object::find()-&gt;where(['id' =&gt; $id])-&gt;with(['objectFacilities'])-&gt;one()){
            foreach($model-&gt;objectFacilities as $facility) array_push($model-&gt;oFacilities, $facility-&gt;id);

            return $this-&gt;render('update', ['model' =&gt; $model]);
        }
    }
}
</code></pre>

<p>Both action use partial view called _form.php</p>

<pre><code>&lt;?php $form = ActiveForm::begin([
    'id' =&gt; 'object-form',
    'options' =&gt; ['class' =&gt; 'form-horizontal'],
    'fieldConfig' =&gt; [
        'template' =&gt; "{label}\n&lt;div class=\"col-lg-9\"&gt;{input}&lt;/div&gt;\n&lt;div class=\"col-lg-9 col-lg-offset-3\"&gt;{error}&lt;/div&gt;",
        'labelOptions' =&gt; ['class' =&gt; 'col-lg-3 control-label'],
    ],
]); ?&gt;

    &lt;?= $form-&gt;field($model, 'oFacilities')-&gt;checkboxList(Facility::forWidget(),[
    'item' =&gt; function($index, $label, $name, $checked, $value) {
        return "&lt;label class='checkbox col-md-4' style='font-weight: normal;'&gt;&lt;input type='checkbox' {$checked} name='{$name}' value='{$value}'&gt;{$label}&lt;/label&gt;";
    }
]) ?&gt;

    &lt;div class="form-group"&gt;
        &lt;div class="col-lg-offset-1 col-lg-11"&gt;
            &lt;?= Html::submitButton($model-&gt;isNewRecord ? 'Create object' : 'Save changes', ['class' =&gt; 'btn btn-primary', 'name' =&gt; 'object-button']) ?&gt;
        &lt;/div&gt;
    &lt;/div&gt;

&lt;?php ActiveForm::end(); ?&gt;
</code></pre>

<p>Create action works fine (both render and save), but I've got an issue with rendering checkbox list based on junktion table - none of checkobxes is checked. Could you tell what I'm doing wrong?</p>

<p>Kind regards,
Kamil
<br><br>
<strong>UPDATE</strong><br><br>
I located the problem - when I change the code which render chckboxList like this: </p>

<pre><code>&lt;?= $form-&gt;field($model, 'oFacilities')-&gt;checkboxList(Facility::forWidget()) ?&gt;
</code></pre>

<p>chosen earlier checkboxes are checked. But then my checkboxList isn't divded to three columns.</p>