1

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

Juan.Queiroz
  • 207
  • 1
  • 3
  • 13

1 Answers1

1

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 --')]) ?>
Irfan Ali
  • 2,238
  • 1
  • 23
  • 22
  • Thanks for your reply. Only the first value stay selected ($ali[0]). So, this solution doesn't work too. – Juan.Queiroz Jun 29 '17 at 04:50
  • You can pass all selected array values like this 'options' => ["1" => ['selected'=>true], "5" => ['selected'=>true]] – Irfan Ali Jun 29 '17 at 05:00
  • here 1 and 5 are options of dropdown. like this you just need to build one array with is format before this and pass that array to field. – Irfan Ali Jun 29 '17 at 05:01
  • I understood, but doesn't work. Only the value $ali[0]->ali_s_id ($ali[0], 'ali_s_id') , stay selected. I have values between 1 and 32. – Juan.Queiroz Jun 29 '17 at 06:00
  • this will help more https://stackoverflow.com/questions/32526601/yii2-htmldropdownlist-and-htmlactivedropdownlist-trade-off – Irfan Ali Jun 29 '17 at 06:03