Skip to content

Instantly share code, notes, and snippets.

@umidjons
Created August 1, 2014 04:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save umidjons/2751077d9d9d639e0f49 to your computer and use it in GitHub Desktop.
Save umidjons/2751077d9d9d639e0f49 to your computer and use it in GitHub Desktop.
Set default value to select in dropDownList

Set default value to select in dropDownList

Options (<option>) attributes set as following:

<?php
$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>
	<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'); ?>
	</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment