Angel Ayala
Angel Ayala

Reputation: 13

Yii2: multiple selection of checkbox list is not saved

I'm having trouble saving options for a checkbox list. When I select more than one option I have the error Answer is invalid and when I select an option it allows me to send the data but I get the error htmlspecialchars() expects parameter 1 to be string, array given

'Answer' => [
    [
        'answer' => '37',
        'question_id' => '34',
    ],
    [
        'answer' => '40',
        'question_id' => '35',
    ],
    [
        'answer' => [ //selected options from checkboxlist
            '43',
            '46',
        ],
        'question_id' => '36',
    ],
    [
        'question_id' => '37',
        'answer' => '42',
    ],
],

Controller

public function actionQuestionnaire($id)
{
    ...... 
    ........

    $count = count(Yii::$app->request->post('Answer', []));

    $answers = [new Answer()];

    for($i = 1; $i < $count; $i++) {
        $answers[] = new Answer();
    }

    if (Model::loadMultiple($answers, Yii::$app->request->post()) && Model::validateMultiple($answers)) {

        foreach ($answers as $answer) {
            $answer->save(false);
        }
        return $this->redirect('index');
    }
    return $this->render('create', [
        'model' => $model,//question data
        'answer' => $answer,
        'dataProvider'=> $dataProvider,
    ]);
}

_form

<?php $form = ActiveForm::begin([
'enableClientValidation' => false,
'enableAjaxValidation' => true,]) ?>

<?= ListView::widget([
    'layout' => '<div class="pull-left">{items}</div>',
    'dataProvider' => $dataProvider,
    'itemView' => function ($model, $key, $index, $widget) use ($form,$answer) {
        return $this->render('_answers',[
            'model' => $model, 
            'answer' => $answer,
            'index' => $index
        ]);
    },
]); ?><div class="form-group">
<?php echo Html::submitButton('<span class="fa fa-plus"></span>'.' '.Yii::t('backend', 'Send') , ['class' => 'btn btn-primary']) ?>

_form_quest

<?php
     $idq = $model->id_question;
     $count = $model->getAnswer($idq);
     echo \yii\helpers\Html::activeHiddenInput($answer, "[$index]question_id",['value' => $model->id_question]); 
     echo  $count > 1?$form->field($answer, "[$index]answer")->checkboxList(ArrayHelper::map($model->questOptions, 'id_option', 'title_option'))->label('') : $form->field($answer, "[$index]answer")->radioList(ArrayHelper::map($model->questOptions, 'id_option', 'title_option'))->label('') ;
                                    ?>  

In order to solve the problem I tried to add the rule ['answer', 'each', 'rule' => ['string']] in the model but it won't let me send the data.

Rules in model

return [
        [['question_id'], 'required'],
        [['pregunta_id'], 'integer'],
        //['answer', 'each', 'rule' => ['string']], //if you used this rule it throws me an error "Answer is invalid" when selecting an option
        [['answer'], 'string'],
    ];

When selecting more than two options

enter image description here

When selecting an option it allows me to send but it shows me the following error

enter image description here

If I only save options from the radioboxlist it allows me to save without problems, the only drawback is with the checkboxlist. I hope you can guide me with this problem.

var_dump to objet

array(4) {

[0]=&gt;
  object(common\models\Answer)#221 (10) {
    ["_attributes":"yii\db\BaseActiveRecord":private]=&gt;
    array(0) {
    }
    ["_oldAttributes":"yii\db\BaseActiveRecord":private]=&gt;
    NULL
    ["_related":"yii\db\BaseActiveRecord":private]=&gt;
    array(0) {
    }
    ["_relationsDependencies":"yii\db\BaseActiveRecord":private]=&gt;
    array(0) {
    }
    ["_errors":"yii\base\Model":private]=&gt;
    NULL
    ["_validators":"yii\base\Model":private]=&gt;
    NULL
    ["_scenario":"yii\base\Model":private]=&gt;
    string(7) "default"
    ["_events":"yii\base\Component":private]=&gt;
    array(0) {
    }
    ["_eventWildcards":"yii\base\Component":private]=&gt;
    array(0) {
    }
    ["_behaviors":"yii\base\Component":private]=&gt;
    array(0) {
    }
  }
  [1]=&gt;
  object(common\models\Answer)#222 (10) {
    ["_attributes":"yii\db\BaseActiveRecord":private]=&gt;
    array(0) {
    }
    ["_oldAttributes":"yii\db\BaseActiveRecord":private]=&gt;
    NULL
    ["_related":"yii\db\BaseActiveRecord":private]=&gt;
    array(0) {
    }
    ["_relationsDependencies":"yii\db\BaseActiveRecord":private]=&gt;
    array(0) {
    }
    ["_errors":"yii\base\Model":private]=&gt;
    NULL
    ["_validators":"yii\base\Model":private]=&gt;
    NULL
    ["_scenario":"yii\base\Model":private]=&gt;
    string(7) "default"
    ["_events":"yii\base\Component":private]=&gt;
    array(0) {
    }
    ["_eventWildcards":"yii\base\Component":private]=&gt;
    array(0) {
    }
    ["_behaviors":"yii\base\Component":private]=&gt;
    array(0) {
    }
  }
  [2]=&gt;
  object(common\models\Answer)#223 (10) {
    ["_attributes":"yii\db\BaseActiveRecord":private]=&gt;
    array(0) {
    }
    ["_oldAttributes":"yii\db\BaseActiveRecord":private]=&gt;
    NULL
    ["_related":"yii\db\BaseActiveRecord":private]=&gt;
    array(0) {
    }
    ["_relationsDependencies":"yii\db\BaseActiveRecord":private]=&gt;
    array(0) {
    }
    ["_errors":"yii\base\Model":private]=&gt;
    NULL
    ["_validators":"yii\base\Model":private]=&gt;
    NULL
    ["_scenario":"yii\base\Model":private]=&gt;
    string(7) "default"
    ["_events":"yii\base\Component":private]=&gt;
    array(0) {
    }
    ["_eventWildcards":"yii\base\Component":private]=&gt;
    array(0) {
    }
    ["_behaviors":"yii\base\Component":private]=&gt;
    array(0) {
    }
  }
  [3]=&gt;
  object(common\models\Answer)#224 (10) {
    ["_attributes":"yii\db\BaseActiveRecord":private]=&gt;
    array(0) {
    }
    ["_oldAttributes":"yii\db\BaseActiveRecord":private]=&gt;
    NULL
    ["_related":"yii\db\BaseActiveRecord":private]=&gt;
    array(0) {
    }
    ["_relationsDependencies":"yii\db\BaseActiveRecord":private]=&gt;
    array(0) {
    }
    ["_errors":"yii\base\Model":private]=&gt;
    NULL
    ["_validators":"yii\base\Model":private]=&gt;
    NULL
    ["_scenario":"yii\base\Model":private]=&gt;
    string(7) "default"
    ["_events":"yii\base\Component":private]=&gt;
    array(0) {
    }
    ["_eventWildcards":"yii\base\Component":private]=&gt;
    array(0) {
    }
    ["_behaviors":"yii\base\Component":private]=&gt;
    array(0) {
    }
  }
}

Upvotes: 1

Views: 453

Answers (1)

Ammar Dje
Ammar Dje

Reputation: 90

well the problem is with your

echo  $count > 1?$form->field($answer, "[$index]answer")->checkboxList(ArrayHelper::map($model->questOptions, 'id_option', 'title_option'))->label('') : $form->field($answer, "[$index]answer")->radioList(ArrayHelper::map($model->questOptions, 'id_option', 'title_option'))->label('') ;
                                    ?>  

you need to set an attribute for each question on your models a thing like that

 [['answer','answer1','answer2','answer3','answer4'], 'string']

Upvotes: 1

Related Questions