Php Yii2:模型未加载数组
我使用以下(简单)代码创建模型,以便与yii2中的表单一起使用:Php Yii2:模型未加载数组,php,yii2,Php,Yii2,我使用以下(简单)代码创建模型,以便与yii2中的表单一起使用: class ChooseAgeCategoriesForm extends Model { public $ageCategories; /** * @return array the validation rules. */ public function rules() { return [ ['ageCategories', 's
class ChooseAgeCategoriesForm extends Model
{
public $ageCategories;
/**
* @return array the validation rules.
*/
public function rules()
{
return [
['ageCategories', 'safe'],
[['ageCategories'], 'each', 'rule' => ['integer']],
];
}
}
但是,尝试如下方式加载模型:
$model = new ChooseAgeCategoriesForm();
if ($model->load(Yii::$app->request->post())) {
$acIDs = $model->ageCategories;
}
代码永远不会执行。实际上,$model->load
返回Bool(false)
,但$model->errors
为空。转储post
的内容会产生以下结果:
array(2) { ["_csrf"]=> string(56) "SzhjdWIuc0oIQSA0BEs4BDp8KzYEXwM5CG80RhNBRDkPUzIXO1gBew==" ["ageCategories"]=> array(9) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" [3]=> string(1) "4" [4]=> string(1) "5" [5]=> string(1) "6" [6]=> string(1) "7" [7]=> string(1) "9" [8]=> string(2) "10" } }
是的,这个字段实际上被发送并填充了一个数字数组。我没看到什么,有什么问题吗?提前谢谢。我还尝试添加['ageCategories','exist','allowArray'=>true],
作为另一条规则,但这也不起作用。有什么想法吗
表格代码
根据要求,表格本身的代码:
<?php $form = ActiveForm::begin([
'action' => Url::to(['/result/team-result']),
'method' => 'post',
'options' => ['id' => 'chooseAgeCategory', 'style' => "margin-bottom: 15px;"]
]); ?>
<?= Select2::widget([
'name' => 'ageCategories',
'id' => 'ac-id',
'value' => $acIDs,
'data' => ArrayHelper::map(AgeCategory::find()->all(), 'id', 'name'),
'options' => ['multiple' => true, 'placeholder' => Yii::t('view', 'Select Age Category')],
'pluginEvents' => [
"change" => "function() { this.form.submit(); }",
]
]) ?>
<?php $form->end(); ?>
我建议您使用
model
属性使用Select2
,以便自动链接到字段,简化加载过程,并使用模型的验证功能:
<?= Select2::widget([
'name' => 'ageCategories',
'id' => 'ac-id',
'value' => $acIDs,
'data' => ArrayHelper::map(AgeCategory::find()->all(), 'id', 'name'),
'options' => ['multiple' => true, 'placeholder' => Yii::t('view', 'Select Age Category')],
'pluginEvents' => [
"change" => "function() { this.form.submit(); }",
]
]) ?>
我建议您使用
model
属性使用Select2
,以便自动链接到字段,简化加载过程,并使用模型的验证功能:
<?= Select2::widget([
'name' => 'ageCategories',
'id' => 'ac-id',
'value' => $acIDs,
'data' => ArrayHelper::map(AgeCategory::find()->all(), 'id', 'name'),
'options' => ['multiple' => true, 'placeholder' => Yii::t('view', 'Select Age Category')],
'pluginEvents' => [
"change" => "function() { this.form.submit(); }",
]
]) ?>
请更改select2小部件代码,如下所示。Yii2使用字段名包装模型名称,如 选择SeageCategoriesForm[年龄类别] 如果你检查你的表格,你就会知道。因此,您必须在select2小部件中给出模型值
<?= Select2::widget([
'model' => $model,
'attribute' => 'ageCategories',
'id' => 'ac-id',
'value' => $acIDs,
'data' => ArrayHelper::map(AgeCategory::find()->all(), 'id', 'name'),
'options' => ['multiple' => true, 'placeholder' => Yii::t('view', 'Select Age Category')],
'pluginEvents' => [
"change" => "function() { this.form.submit(); }",
]
]) ?>
参考:请更改select2小部件代码,如下所示。Yii2使用字段名包装模型名称,如 选择SeageCategoriesForm[年龄类别] 如果你检查你的表格,你就会知道。因此,您必须在select2小部件中给出模型值
<?= Select2::widget([
'model' => $model,
'attribute' => 'ageCategories',
'id' => 'ac-id',
'value' => $acIDs,
'data' => ArrayHelper::map(AgeCategory::find()->all(), 'id', 'name'),
'options' => ['multiple' => true, 'placeholder' => Yii::t('view', 'Select Age Category')],
'pluginEvents' => [
"change" => "function() { this.form.submit(); }",
]
]) ?>
Ref:显示表单代码将表单代码添加到问题中。变量
$model
的类是什么?$model
初始化为$model=new ChooseAgeCategoriesForm()代码>,因此,ChooseAgeCategoriesForm
源自yii\base\Model
。显示表单的代码将表单的代码添加到问题中。变量$Model
的类是什么?$Model
初始化为$Model=new ChooseAgeCategoriesForm()代码>,因此选择源于yii\base\Model
的seagecategoriesform
。