带数组的活动表单内的字段
Field inside Active Form with Array
我有一个包含多个 select 的 dropDownList,当我进入 actionUpdate 时,我想要 dropDownList 的内容 selected(正确的内容)。
数组:
array (size=3)
0 =>
object(backend\models\AliHasAliPlanRef)[100]
private '_attributes' (yii\db\BaseActiveRecord) =>
array (size=3)
'ali_s_id' => int 23
'ali_plan_ref_id' => int 50
'weight' => int 22
private '_oldAttributes' (yii\db\BaseActiveRecord) =>
array (size=3)
'ali_s_id' => int 23
'ali_plan_ref_id' => int 50
'weight' => int 22
private '_related' (yii\db\BaseActiveRecord) =>
array (size=0)
empty
private '_errors' (yii\base\Model) => null
private '_validators' (yii\base\Model) => null
private '_scenario' (yii\base\Model) => string 'default' (length=7)
private '_events' (yii\base\Component) =>
array (size=0)
empty
private '_behaviors' (yii\base\Component) =>
array (size=0)
empty
1 =>
object(backend\models\AliHasAliPlanRef)[105]
private '_attributes' (yii\db\BaseActiveRecord) =>
array (size=3)
'ali_s_id' => int 24
'ali_plan_ref_id' => int 50
'weight' => int 33
private '_oldAttributes' (yii\db\BaseActiveRecord) =>
array (size=3)
'ali_s_id' => int 24
'ali_plan_ref_id' => int 50
'weight' => int 33
private '_related' (yii\db\BaseActiveRecord) =>
array (size=0)
empty
private '_errors' (yii\base\Model) => null
private '_validators' (yii\base\Model) => null
private '_scenario' (yii\base\Model) => string 'default' (length=7)
private '_events' (yii\base\Component) =>
array (size=0)
empty
private '_behaviors' (yii\base\Component) =>
array (size=0)
empty
2 =>
object(backend\models\AliHasAliPlanRef)[106]
private '_attributes' (yii\db\BaseActiveRecord) =>
array (size=3)
'ali_s_id' => int 25
'ali_plan_ref_id' => int 50
'weight' => int 4
private '_oldAttributes' (yii\db\BaseActiveRecord) =>
array (size=3)
'ali_s_id' => int 25
'ali_plan_ref_id' => int 50
'weight' => int 4
private '_related' (yii\db\BaseActiveRecord) =>
array (size=0)
empty
private '_errors' (yii\base\Model) => null
private '_validators' (yii\base\Model) => null
private '_scenario' (yii\base\Model) => string 'default' (length=7)
private '_events' (yii\base\Component) =>
array (size=0)
empty
private '_behaviors' (yii\base\Component) =>
array (size=0)
empty
实际上我有这段代码(并且有效,但仅适用于数组的第一项 selected):
<?=
$form->field($ali[0], 'ali_s_id', ['template' => '{label}{input}<span class="help-block">{hint}{error}</span>'])->dropDownList(Category::getHierarchy(), ['size' => 10, 'multiple' => 'multiple'], ['prompt' => Yii::t('app', '-- Select --'),
])
?>
以上代码示例:
我想要 select 从 DropDownList 中选择的所有(正确)项目。我也试过了:
<?=
$form->field($ali, 'ali_s_id[]', ['template' => '{label}{input}<span class="help-block">{hint}{error}</span>'])->dropDownList(Category::getHierarchy(), ['size' => 10, 'multiple' => 'multiple'], ['prompt' => Yii::t('app', '-- Select --'),
])
?>
但是我收到一个错误:
Call to a member function isAttributeRequired() on array
预期结果:
您必须正确分配已选择的值。你可以通过两种方式做到这一点。
$ali[0]->ali_s_id = [23, 24, 25];
或
$selectedValues = ["23" => ['selected'=>true], "24" => ['selected'=>true], "25" => ['selected'=>true] ];
<?= $form->field($ali[0], 'ali_s_id', ['template' => '{label}{input}<span class="help-block">{hint}{error}</span>'])->dropDownList(Category::getHierarchy(), ['size' => 10, 'multiple' => 'multiple'],['options' => $selectedValues], 'prompt' => Yii::t('app', '-- Select --')]) ?>
我有一个包含多个 select 的 dropDownList,当我进入 actionUpdate 时,我想要 dropDownList 的内容 selected(正确的内容)。
数组:
array (size=3)
0 =>
object(backend\models\AliHasAliPlanRef)[100]
private '_attributes' (yii\db\BaseActiveRecord) =>
array (size=3)
'ali_s_id' => int 23
'ali_plan_ref_id' => int 50
'weight' => int 22
private '_oldAttributes' (yii\db\BaseActiveRecord) =>
array (size=3)
'ali_s_id' => int 23
'ali_plan_ref_id' => int 50
'weight' => int 22
private '_related' (yii\db\BaseActiveRecord) =>
array (size=0)
empty
private '_errors' (yii\base\Model) => null
private '_validators' (yii\base\Model) => null
private '_scenario' (yii\base\Model) => string 'default' (length=7)
private '_events' (yii\base\Component) =>
array (size=0)
empty
private '_behaviors' (yii\base\Component) =>
array (size=0)
empty
1 =>
object(backend\models\AliHasAliPlanRef)[105]
private '_attributes' (yii\db\BaseActiveRecord) =>
array (size=3)
'ali_s_id' => int 24
'ali_plan_ref_id' => int 50
'weight' => int 33
private '_oldAttributes' (yii\db\BaseActiveRecord) =>
array (size=3)
'ali_s_id' => int 24
'ali_plan_ref_id' => int 50
'weight' => int 33
private '_related' (yii\db\BaseActiveRecord) =>
array (size=0)
empty
private '_errors' (yii\base\Model) => null
private '_validators' (yii\base\Model) => null
private '_scenario' (yii\base\Model) => string 'default' (length=7)
private '_events' (yii\base\Component) =>
array (size=0)
empty
private '_behaviors' (yii\base\Component) =>
array (size=0)
empty
2 =>
object(backend\models\AliHasAliPlanRef)[106]
private '_attributes' (yii\db\BaseActiveRecord) =>
array (size=3)
'ali_s_id' => int 25
'ali_plan_ref_id' => int 50
'weight' => int 4
private '_oldAttributes' (yii\db\BaseActiveRecord) =>
array (size=3)
'ali_s_id' => int 25
'ali_plan_ref_id' => int 50
'weight' => int 4
private '_related' (yii\db\BaseActiveRecord) =>
array (size=0)
empty
private '_errors' (yii\base\Model) => null
private '_validators' (yii\base\Model) => null
private '_scenario' (yii\base\Model) => string 'default' (length=7)
private '_events' (yii\base\Component) =>
array (size=0)
empty
private '_behaviors' (yii\base\Component) =>
array (size=0)
empty
实际上我有这段代码(并且有效,但仅适用于数组的第一项 selected):
<?=
$form->field($ali[0], 'ali_s_id', ['template' => '{label}{input}<span class="help-block">{hint}{error}</span>'])->dropDownList(Category::getHierarchy(), ['size' => 10, 'multiple' => 'multiple'], ['prompt' => Yii::t('app', '-- Select --'),
])
?>
以上代码示例:
我想要 select 从 DropDownList 中选择的所有(正确)项目。我也试过了:
<?=
$form->field($ali, 'ali_s_id[]', ['template' => '{label}{input}<span class="help-block">{hint}{error}</span>'])->dropDownList(Category::getHierarchy(), ['size' => 10, 'multiple' => 'multiple'], ['prompt' => Yii::t('app', '-- Select --'),
])
?>
但是我收到一个错误:
Call to a member function isAttributeRequired() on array
预期结果:
您必须正确分配已选择的值。你可以通过两种方式做到这一点。
$ali[0]->ali_s_id = [23, 24, 25];
或
$selectedValues = ["23" => ['selected'=>true], "24" => ['selected'=>true], "25" => ['selected'=>true] ];
<?= $form->field($ali[0], 'ali_s_id', ['template' => '{label}{input}<span class="help-block">{hint}{error}</span>'])->dropDownList(Category::getHierarchy(), ['size' => 10, 'multiple' => 'multiple'],['options' => $selectedValues], 'prompt' => Yii::t('app', '-- Select --')]) ?>