Hi,
I’ve got a problem with checkboxList in update action. I’ve got Two models: Object and Facility which are in may-to-many relation via objectsfacilities junktion table.
Object model:
class Object extends ActiveRecord
{
public $oFacilities = array();
public static function tableName()
{
return 'objects';
}
public function getObjectFacilities(){
return $this->hasMany(Facility::className(),['id' => 'facilityId'])->viaTable('objectsfacilities', ['objectId' => 'id']);
}
}
Facility model:
class Facility extends ActiveRecord
{
public static function tableName()
{
return 'facilities';
}
public static function forWidget(){
$facilities =static::find()->select(['id','name'])->where(['type' => static::TYPE_OBJECT])
->orderBy('name')
->asArray()
->all();
return ArrayHelper::map($facilities, 'id', 'name');
}
public function getObjectsWithFacility(){
return $this->hasMany(Object::className(), ['id' => 'objectId'])->viaTable('objectsfacilities', ['facilityId' => 'id']);
}
}
In my ObjectController I’ve got two actions: create (which works fine) and update (for now I just wont to render a form with values of chosen model):
class ObjectController extends Controller
{
public function actionCreate(){
$model = new Object();
if($model->load(Yii::$app->request->post()) && $model->validate()){
$model->save();
foreach($model->oFacilities as $id){
$facility = Facility::findOne($id);
$model->link('objectFacilities', $facility);
}
Yii::$app->session->setFlash('alert', 'object-created');
return $this->redirect('index');
}
return $this->render('create', ['model' => $model]);
}
public function actionUpdate($id){
/* This action is complete - for now I just want to render a form */
if($model = Object::find()->where(['id' => $id])->with(['objectFacilities'])->one()){
foreach($model->objectFacilities as $facility) array_push($model->oFacilities, $facility->id);
return $this->render('update', ['model' => $model]);
}
}
}
Both actions use partial view named _form.php:
<?php $form = ActiveForm::begin([
'id' => 'object-form',
'options' => ['class' => 'form-horizontal'],
'fieldConfig' => [
'template' => "{label}\n<div class=\"col-lg-9\">{input}</div>\n<div class=\"col-lg-9 col-lg-offset-3\">{error}</div>",
'labelOptions' => ['class' => 'col-lg-3 control-label'],
],
]); ?>
<?= $form->field($model, 'oFacilities')->checkboxList(Facility::forWidget(),[
'item' => function($index, $label, $name, $checked, $value) {
return "<label class='checkbox col-md-4' style='font-weight: normal;'><input type='checkbox' {$checked} name='{$name}' value='{$value}'>{$label}</label>";
}
]) ?>
<div class="form-group">
<div class="col-lg-offset-1 col-lg-11">
<?= Html::submitButton($model->isNewRecord ? 'Create object' : 'Save changes', ['class' => 'btn btn-primary', 'name' => 'object-button']) ?>
</div>
</div>
<?php ActiveForm::end(); ?>
And the problem is with checkboxList in this view. When I change the code which is generating checkboxList to:
<?= $form->field($model, 'oFacilities')->checkboxList(Facility::forWidget()) ?>
chosen earlier checkboxes are checked. But I want them to be generated as three columns, so I’m using an anonnymous function for ‘item’ key in options table. But then none of checkboxes is checked.
Yii version: 2.0.8-dev
PHP: 7.0.5
Server: Apache 2.4.20 with php in fcgid mode (2.3.9)
Kind regards,
Kamil