【发布时间】:2020-11-20 05:13:00
【问题描述】:
我无法保存复选框列表的选项。当我选择多个选项时,出现错误 Answer is invalid,当我选择一个选项时,它允许我发送数据,但出现错误 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',
],
],
控制器
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('') ;
?>
为了解决问题,我尝试在模型中添加规则['answer', 'each', 'rule' => ['string']],但它不允许我发送数据。
模型中的规则
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'],
];
在选择两个以上的选项时
选择一个选项时,它允许我发送,但它显示了以下错误
如果我只保存单选框列表中的选项,它允许我毫无问题地保存,唯一的缺点是复选框列表。我希望你能指导我解决这个问题。
var_dump 到对象
array(4) {
[0]=>
object(common\models\Answer)#221 (10) {
["_attributes":"yii\db\BaseActiveRecord":private]=>
array(0) {
}
["_oldAttributes":"yii\db\BaseActiveRecord":private]=>
NULL
["_related":"yii\db\BaseActiveRecord":private]=>
array(0) {
}
["_relationsDependencies":"yii\db\BaseActiveRecord":private]=>
array(0) {
}
["_errors":"yii\base\Model":private]=>
NULL
["_validators":"yii\base\Model":private]=>
NULL
["_scenario":"yii\base\Model":private]=>
string(7) "default"
["_events":"yii\base\Component":private]=>
array(0) {
}
["_eventWildcards":"yii\base\Component":private]=>
array(0) {
}
["_behaviors":"yii\base\Component":private]=>
array(0) {
}
}
[1]=>
object(common\models\Answer)#222 (10) {
["_attributes":"yii\db\BaseActiveRecord":private]=>
array(0) {
}
["_oldAttributes":"yii\db\BaseActiveRecord":private]=>
NULL
["_related":"yii\db\BaseActiveRecord":private]=>
array(0) {
}
["_relationsDependencies":"yii\db\BaseActiveRecord":private]=>
array(0) {
}
["_errors":"yii\base\Model":private]=>
NULL
["_validators":"yii\base\Model":private]=>
NULL
["_scenario":"yii\base\Model":private]=>
string(7) "default"
["_events":"yii\base\Component":private]=>
array(0) {
}
["_eventWildcards":"yii\base\Component":private]=>
array(0) {
}
["_behaviors":"yii\base\Component":private]=>
array(0) {
}
}
[2]=>
object(common\models\Answer)#223 (10) {
["_attributes":"yii\db\BaseActiveRecord":private]=>
array(0) {
}
["_oldAttributes":"yii\db\BaseActiveRecord":private]=>
NULL
["_related":"yii\db\BaseActiveRecord":private]=>
array(0) {
}
["_relationsDependencies":"yii\db\BaseActiveRecord":private]=>
array(0) {
}
["_errors":"yii\base\Model":private]=>
NULL
["_validators":"yii\base\Model":private]=>
NULL
["_scenario":"yii\base\Model":private]=>
string(7) "default"
["_events":"yii\base\Component":private]=>
array(0) {
}
["_eventWildcards":"yii\base\Component":private]=>
array(0) {
}
["_behaviors":"yii\base\Component":private]=>
array(0) {
}
}
[3]=>
object(common\models\Answer)#224 (10) {
["_attributes":"yii\db\BaseActiveRecord":private]=>
array(0) {
}
["_oldAttributes":"yii\db\BaseActiveRecord":private]=>
NULL
["_related":"yii\db\BaseActiveRecord":private]=>
array(0) {
}
["_relationsDependencies":"yii\db\BaseActiveRecord":private]=>
array(0) {
}
["_errors":"yii\base\Model":private]=>
NULL
["_validators":"yii\base\Model":private]=>
NULL
["_scenario":"yii\base\Model":private]=>
string(7) "default"
["_events":"yii\base\Component":private]=>
array(0) {
}
["_eventWildcards":"yii\base\Component":private]=>
array(0) {
}
["_behaviors":"yii\base\Component":private]=>
array(0) {
}
}
}
【问题讨论】:
-
兄弟尝试使用 die() 和 print_r() 你的变量让我们看到它呈现的内容也许你正在尝试获取一个对象而不是一个字符串
-
如果我使用 print_r 或 var_dump 它会显示错误
htmlspecialchars() expects parameter 1 to be string, array given -
不,在你尝试迭代你的对象第一件事之前,die() 和 print_r() 你的对象
-
我得到了以下
标签: php arraylist activerecord yii2