Select2 Widget.. Tags From Database


I have few question regarding select2 widget from kartik

is it possible to load the tag directly from database table… if how ??

need only my tags from my database not let enter the user tags what they want… to avoid spelling mistakes… is there any config possible ?

i have tryed on this way but not working

<?php $data = Sprachen::find()->select('sprache')->asArray()->all() ?>

<?=  $form->field($model, 'sprachen')->widget(Select2::classname(),[

    		name' => 'sprachen',

    		'options' => ['placeholder' => 'Bitte auswählen...', 'class' => 'form-controler'],

    		'pluginOptions' => ['tags' => array_merge(["" => "", $data])]


    		]) ?>


Ensure the [font="Lucida Console"]tags[/font] should return a single dimensional array (and not an associative array) something like [font="Lucida Console"]["red", "blue", "green"][/font]. Check the details at the plugin site.

In additon, note that when you are using the TAGS method the values are NOT STORED as an ARRAY. Instead its stored as a COMMA SEPARATED text (or any separator you choose).

So remember to pass the value from your model accordingly to the input if you wants tags shown preselected. Similarly, when you read you need to convert the comma separated tag list to an array and store if needed.

thanks i made now one dimension array, and it working.

How can i make it so that user cant enter their own tags ???

<?php $sprachen  = ArrayHelper::getColumn(Sprachen::find()->select('sprache')->all(), 'sprache'); ?>

<?=  $form->field($model, 'sprachen')->widget(Select2::classname(),[

    			'name' => 'sprachen',

    			'options' => ['placeholder' => 'Bitte auswählen...', 'class' => 'form-controler'],

    			'pluginOptions' => ['tags' => $sprachen]


 ]) ?>

One way to do it is override the [font="Lucida Console"]createSearchChoice[/font] in [font="Lucida Console"]pluginOptions[/font]… read more about this on plugin site:

'pluginOptions' => [

    'createSearchChoice' => new JsExpression('function() { return null; }'),


However, the better way IMO, would be to use a normal multiple selection list, which will appear like tags in Select2 widget, but will allow only specific defined tags to be selected. just define it like a normal dropdown list with


tryed this but still no change…

you have one example where its showcased

// Without model and implementing a multiple select

echo '<label class="control-label">Provinces</label>';

echo Select2::widget([

    'name' => 'state_10', 

    'data' => $data,

    'options' => [

        'placeholder' => 'Select provinces ...', 


        'multiple' => true



i tryed to implement on this way but was not working showing me only a dropdown menu

Should work as mentioned in the demo example (and as you can see on the demo site)…

you may want to post your code snippet

<?=  $form->field($model, 'sprachen')->widget(Select2::classname(),[

				'name' => 'sprachen', 

				'data' => $sprachen,

				'options' => [

				'placeholder' => 'Select provinces ...', 


				'multiple' => true



    			]) ?>

this is my code…

i have attached a pic how it looks like

Seems some javascript error on your client – as the plugin is not initialized – can you get the Javascript error from your browser error console (e.g. using Firebug for mozilla).

Hi Ter,

Just for sharing.

my code works well for saving and preload data which is already saved in db.




	//these are the data I need to preload from DB table

	$arrayVal  = explode(',',$model->listofparties);//split as array

	$arrayFlip = array_flip($arrayVal); //reverse key =>value


	//List organisation items/data

 	$matchOrg = CHtml::listData(Organisation::model()->findAll('active =1'), 'id','name');

	//match from actual items/data with data in db table, retrieve which is match only

	$arrayFinalMatch = (array_intersect_key($matchOrg, $arrayFlip));//get macth key and retrieve value



	//set as selected value.

	$selected = $arrayFinalMatch; 

	$options = array();

	foreach ($selected as $key => $value) { 

	//here I create an array suitable for the 'options' attribute with my data marked as selected





  	// Working with model




  'data'=>CHtml::listData(Organisation::model()->findAll('active =1'), 'id','name'),


  'htmlOptions' => array(

      'options'=>$options, //the selected values

		'multiple' => true,

		'placeholder' => 'Type here ...',


	'options' => array(

		'width' => '90%',


		//'maximumSelectionSize' => 8,

		'tokenSeparators' => array(',', ' ')






in Controller

//TbSelect2 for the list parties for Tower

 				if(is_array($model->listofparties)){//check is data is array (value is selected)

				$model->listofparties = implode(",",$model->listofparties);		

 				}else{//check is data is NOT array (value is NOT selected- empty)

 				$model->listofparties = '';	


I hope my code will help others to stop cracking head…! haha

Yii is awesome…