Solved. activeListBox + multiple

Can I create ListBox element with multiple selected?

CODE:

$form->cat_writers = array('1', '2');


$form->groups = array(


  '1' => 'group1',


  '2' => 'group2',


  '3' => 'group3'


)


echo CHtml::activeListBox($form, 'cat_writers', $form->groups, array('multiple'=>''));

RESULT:



<select multiple="multiple" size="4" name="Pages_frm_cat_add_edit[cat_writers]">


<option value="1" selected>group1</option>


<option value="2" selected>group2</option>


<option value="3">group3</option>


</select>


I WANT:



<select multiple size="4" name="Pages_frm_cat_add_edit[cat_writers][]">


...


</select>


Does the currently generated code work for you?

The code that you want is NOT xhtml compatible.

Maybe I was not properly reflected. The generated code allows you to send only 1 value <name="Pages_frm_cat_add_edit[cat_writers]">, but I need to pass an array <name="Pages_frm_cat_add_edit[cat_writers][]">

Good Morning,

I know the problem, he is currently dealing with, from my asmselectex extension.


Example 1:

If you use the activeDropDownList as suggested:



<?php echo CHtml::activeDropDownList($model, $attribute, $data, $htmlOptions); ?>


the form element, may looks like this:



<select size="4" multiple="multiple" name="User[roles]" id="User_roles">


<option value="user">User</option>


<option value="manager">Manager</option>


</select>


but the submitted data, will look like this:



Array


(


    [User] => Array


        (


            [roles] => manager


        )


)

even when the user, did select multiple options from the select form element!


Example 2:

A way to have multiple selections, would be to to append brackets to the $attribute-property:

<?php echo CHtml::activeDropDownList($model, $attribute.'[]', $data, $htmlOptions); ?>


the form element, would then look like this:



<select size="4" multiple="multiple" name="User[][roles]" id="User_roles">


<option value="user">User</option>


<option value="manager">Manager</option>


</select>


but the submitted data, will look like this:



Array


(


    [User] => Array


        (


            [0] => Array


                (


                    [roles] => user


                )


            [1] => Array


                (


                    [roles] => manager


                )


        )


)


when the user, did select multiple options from the select form element.


Example 3:

In the case, that dotpack and me want to create

<select size="4" multiple="multiple" name="User[roles][]" id="User_roles">


<option value="user">User</option>


<option value="manager">Manager</option>


</select>


the submitted data, would be ordered in the $_POST request, like shown below

Array


(


    [User] => Array


        (


            [roles] => Array


                (


                    [0] => user


                    [1] => manager


                )





        )





)

Maybe there is already a way to create this case, but after looking at CHtml::resolveName, I think there isnt.

greets ironic


//edit:

there is a way to create this:



<?php


$htmlOptions['name'] = get_class($model).'['.$attribute.']'.'[]';


echo CHtml::activeDropDownList($model, $attribute, $data, $htmlOptions);


?>


This would prevent that CHtml::resolveNameID gets the name-attribute from CHtml::resolveName.

I see. Just fixed this. Thanks!

Thanks!