duanchu7271 2016-04-27 22:21
浏览 139
已采纳

选中Yii2 checkboxList中的项目

I've got a dummy problem with checkboxList in ActiveForm.

So I've got two models (I cut off irrelevant code):

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']);
    }
}

And I've got two actions in my ObjectController:

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 action use partial view called _form.php

<?php $form = ActiveForm::begin([
    'id' => 'object-form',
    'options' => ['class' => 'form-horizontal'],
    'fieldConfig' => [
        'template' => "{label}
<div class=\"col-lg-9\">{input}</div>
<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(); ?>

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?

Kind regards, Kamil

UPDATE

I located the problem - when I change the code which render chckboxList like this:

<?= $form->field($model, 'oFacilities')->checkboxList(Facility::forWidget()) ?>

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

  • 写回答

1条回答 默认 最新

  • duanpu2272 2016-04-29 06:47
    关注

    The {checked} part of your item functions outputs a 1 or a 0 in the input element.

    Change the value to an actual 'checked' string to fix your problem.

    <?=
        $form->field($model, 'oFacilities')->checkboxList(Facility::forWidget(),[
            'item' => function($index, $label, $name, $checked, $value) {
                $checked = $checked ? 'checked' : '';
                return "<label class='checkbox col-md-4' style='font-weight: normal;'><input type='checkbox' {$checked} name='{$name}' value='{$value}'>{$label}</label>";
            }
        ])
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Stata 面板数据模型选择
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用