Yii2中的已检查项目checkboxList

时间:2016-04-27 22:21:44

标签: php yii2

我在ActiveForm中遇到了checkboxList的虚拟问题。

所以我有两个模型(我切断了不相关的代码):

对象模型:

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

设施模型:

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

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

这两个动作都使用名为_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(); ?>

创建动作很好(渲染和保存),但是我在基于junktion表的渲染复选框列表时出现问题 - 没有选中任何复选框。你能说出我做错了吗?

亲切的问候, 卡米尔

更新

我找到了问题 - 当我更改像这样渲染chckboxList的代码时:

<?= $form->field($model, 'oFacilities')->checkboxList(Facility::forWidget()) ?>
选中之前选中的

复选框。但是我的checkboxList不会被分成三列。

1 个答案:

答案 0 :(得分:3)

项目函数的{checked}部分在输入元素中输出1或0。

将值更改为实际的“已检查”字符串以解决问题。

<?=
    $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>";
        }
    ])
?>