带有数组的活动表格内的字段

时间:2017-06-29 02:57:18

标签: php yii2 yii2-advanced-app yii2-model

我有一个带有多个select的dropDownList,当我转到actionUpdate时,我想选择dropDownList的内容(正确的内容)。

阵列:

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

实际上我有这个代码(并且有效,但仅适用于选择数组的第一项):

<?=
$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 --'),
])
?>

上述代码示例:

Example of the code above

我想选择从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

预期结果:

Result Expected

1 个答案:

答案 0 :(得分:1)

您必须正确分配aleardy选定值。你可以这两种方式做到这一点。

$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 --')]) ?>