[Solved] Options in activeDropDownList()

I have added an extra option to activeDropDownList by adding an an array into the $htmlOptions like the following

array('prompt'=>'None')

However, as the document states, this will leave the value as empty. I need this value to be set to '0'.

This should be done using 'options' instead of 'prompt' but when trying this way the None field is no longer present in the list at all. Following is the code I am using.

 array('options'=>array('prompt'=>'None', 'value'=>'0'))

Is anyone able to point out how this is incorrect?

Thanks.

'options' only applies to normal list options. In your case, it is better you "insert" the prompt option in front of all other normal options.

try this

activeDropDownList($model,'drop1',array('name'=>'drop1','prompt'=>'none'))

Thanks again for the reply qiang but the documentation does not state that 'insert' is a valid value to put here. I did try it but it did not fix the problem.

murugan,

Your suggestions is how I initially had it but that method does not let me specify a value for None.

Hi,

i think qiang ment to insert the 'prompt' option in your normal list of options on first place before you use them as a parameter for activeDropDownList…

OK,

I just realised that I need to explain this a little better. The value needs to be passed as an integer, not a string.

I need to option to print as follows:

<option value="0">None</option>

The important part here is not the 'None' but the value being an integer of zero. As stated in the documentation the prompt $htmlOptions leaves the value empty.

By using prompt for the $htmlOptions I am left with

<option value="">None</option>

I think that qiang and yoshi are telling NOT to use the prompt option, but insert this value in the actual data.

$data = array('0' => 'None', 'key1' => 'Somthing', 'key2' => 'Something else…');

Thanks all to who have helped me here. I have solved it.

I created a new method in the controller called getParents() as below:

	function getParents()


	{


		$parents = array('0'=>'None');


		$parents = array_merge($parents, CHtml::listData(Page::model()->findAll() , 'id', 'title'));


				


		return $parents;


	}

I then pass the returned value to activeDropDownList() in the form.

Update:

This post wasn’t as resolved as I thought. array_merge() will actually reset the array indexes to 0,1,2,3 etc. rather than keep the table id as required. Therefore the code should be changed as:


		$parents = array('0'=>'None');

		$parents = $parents + CHtml::listData(MenuItems::model()->findAll() , 'id', 'name');

		

		return $parents;

I didn’t realise you just add arrays like that. Hope this helps anyone looking for help on this topic.

This is exactly my problem. Can I said this is the practice way to solve this issue? Or there exist a better way? :)