[solved] dropDownList add value to "prompt"?

Hi Guys!

Just for understanding:

Is is possible to add a default value to the "prompt" of a DropDownList?

If yes - what is the correct way?

For example I have:




echo Html::dropDownList(

    'myDropDown', 

    null, 

    [

        1 => 'Item 1',

        2 => 'Item 2',

        3 => 'Item 3',

    ], 

    ['prompt' => 'Select Item']	

);



I tried for example:




echo Html::dropDownList(

    'myDropDown', 

    null, 

    [

        1 => 'Item 1',

        2 => 'Item 2',

        3 => 'Item 3',

    ], 

    [

        'prompt' => [

            'label' => 'Select Category', 

            'value' => 'promtValue'

        ]

    ]	

);



But prompt seems to accept only a string?

Is there a way to add a default value to a prompt?

Or do I have to do something like:




echo Html::dropDownList(

    'myDropDown', 

    null,

    array_merge(

      ['promtValue' => 'please select'], 

      [

        1 => 'Item 1',

        2 => 'Item 2',

        3 => 'Item 3',

      ]

    )

);



What is the proposed "correct" way?

Thanks and best regards

There is no way of doing it without creating your own method for dropdown rendering or extending Html::renderSelectOptions.

The easiest solution is just to add first selectable option to $items array with the value you want as a prompt message.

1 Like

Okay I understand.

Thank you very much.