Search code examples
phpyii2yii2-advanced-appyii2-model

Field inside Active Form with Array


I have a dropDownList with multiple select and when i goes to the actionUpdate, i wanna the content of dropDownList selected (correct content).

Array:

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

Actually i have this code (and works, but only for the first item selected of array):

<?=
$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:

Example of the code above

I wanna select all (correct) items seletected from DropDownList. I tried too:

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

But i received an error:

Call to a member function isAttributeRequired() on array

Result expected:

Result Expected


Solution

  • you have to assign aleardy selected values properly. you can do this two ways.

    $ali[0]->ali_s_id = [23, 24, 25];
    

    or

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