Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Set default value to select in dropDownList

Set default value to select in dropDownList

Options (<option>) attributes set as following:

$form->dropDownList($model,'attr',$data,['options'=>['value1'=>['attr1'=>'attr1 val', 'attr2'=>'attr2 val',]]]);

I want to select option with value 860, like so:

<option value=860 selected>Uzb</option>

I can use following code to achieve this (R::country() and R::region() returns list data in array(val=>text,...) format):

<div class="form-group">
	<div class="col-sm-4">
		<?= $form->label($model, 'COUNTRY'); ?>
		<?= $form->dropDownList($model, 'COUNTRY', R::country(), ['class' => 'form-control', 'options' => ['860' => ['selected' => true],]]); ?>
		<?= $form->error($model, 'COUNTRY'); ?>
	<div class="col-sm-4">
		<?= $form->label($model, 'REGION'); ?>
		<?= $form->dropDownList($model, 'REGION', R::region(), ['class' => 'form-control', 'options' => ['26' => ['selected' => true],]]); ?>
		<?= $form->error($model, 'REGION'); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.