如何在Yii2中保存Dropdownlist和Checkboxlist

时间:2018-11-19 16:49:43

标签: javascript yii2

我创建了一个复选框列表,如下所示:

checkboxlist

我希望用户能够选择从属下拉列表并选中主题的任何复选框。

        public function actionCreate()
    {
        $model = new AcaClassSubjects();

        if ($model->load(Yii::$app->request->post()) && isset($_POST['AcaClassSubjects'])) {

        $model->attributes = $_POST['AcaClassSubjects'];
        for($i=0;$i<count($_REQUEST['AcaClassSubjects']['class_subject_subject_id']);$i++) :
            $model->class_subject_id = NULL;
            $model->isNewRecord = true;
            $model->class_subject_subject_id = $_POST['AcaClassSubjects']['class_subject_subject_id'][$i];
            $model->class_subject_class_group_id = $_POST['AcaClassSubjects']['class_subject_class_group_id'];
            $model->class_subject_class_id = $_POST['AcaClassSubjects']['class_subject_class_id'];
            $model->created_by = Yii::$app->getid->getId(); 
            $model->created_at = new \yii\db\Expression('NOW()');
            if($model->save()) {
                  Yii::$app->session->setFlash('green-'.$i, '<i class="fa fa-info-circle"></i> <b>Class:</b> '.AcaClassMaster::findOne($_POST['AcaClassSubjects']['class_subject_class_id'])->class_name.' for <b>Subject: </b>'.AcaSubjects::findOne($_POST['AcaClassSubjects']['class_subject_class_id'][$i])->subject_name.' is created successfully');
            } else {
                  Yii::$app->session->setFlash('red-'.$i, '<i class="fa fa-warning"></i> The combination of <b>Class:</b> '.AcaClassMaster::findOne($_POST['AcaClassSubjects']['class_subject_class_id'])->class_name.' and <b>Subject: </b>'.AcaSubjects::findOne($_POST['AcaClassSubjects']['class_subject_class_id'][$i])->subject_name.' has already been taken.');
            }   
        endfor;
    
            return $this->redirect(['index']);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

查看

<div class="col-xs-12 col-lg-12">
    <div class="<?php echo $model->isNewRecord ? 'box-success' : 'box-info'; ?> box view-item col-xs-12 col-lg-12">
        <div class="academic-subject-form">
            <?php $form = ActiveForm::begin([
                    'id' => 'academic-subject-form',
                    'enableAjaxValidation' => false,
                    'fieldConfig' => [
                        'template' => "{label}{input}{error}",
                    ],
            ]); ?>    


            <div class="col-xs-12 col-lg-12 no-padding">  
                <div id="root-container-id" class="col-xs-12 col-sm-12 col-lg-12">   

                    <?=
                        $form->field($model, 'class_subject_subject_id')->checkboxList(AcaSubjects::forWidget(),[
                            'item' => function($index, $label, $name, $checked, $value) {
                                $checked = $checked ? 'checked' : '';
                                //'class' => 'check-all';
                                return "<label class='checkbox col-md-4' style='font-weight: normal;'><input type='checkbox' {$checked} name='{$name}' value='{$value}'>{$label}</label>";
                            }
                        ])->label('Subjects <label><input type="checkbox" id="root-container-id">Check All</label>');
                    ?>                                                  
                </div>
            </div>                       


            <div class="form-group col-xs-12 col-sm-6 col-lg-4 no-padding">
                <div class="col-xs-6">
                    <?= Html::submitButton($model->isNewRecord ? Yii::t('aca', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord  ? 'btn btn-block btn-success' : 'btn btn-block btn-info']) ?>
                </div>
                <div class="col-xs-6">
                    <?= Html::a(Yii::t('academic', 'Cancel'), ['index'], ['class' => 'btn btn-default btn-block']) ?>
                </div>
            </div>
            <?php ActiveForm::end(); ?>
        </div>
    </div>
</div> 

错误如下图所示: offset error

我有以下问题: 1.为什么会出现此错误

  

未初始化的字符串偏移量:1

  1. 它仅保存2行

  2. 复选框列表的所有复选框不起作用

  3. 如果用户未选中任何复选框,如何显示对话框?

0 个答案:

没有答案