Add create new link in Yii Framework drop down list

I am a newbie in Yii Framework and doing a small application in Yii Framework, I have the database for Invoice and Customers like this

    ==== Invoice ====









       === Customers ===







Now as per my requirment I need all the available customer name should come in a dropdown list in Invoice create form so I made changes in Invoice form.php to call all the available customers name like this

<div class="row">

    <?php echo $form->labelEx($customers,'customer_name'); ?>

    <?php echo $form->dropdownList($customers,'customer_name', CHtml::listData(Customers::model()->findAll(), 'id', 'customer_name'), array('empty'=>'Choose one')); ?>

    <?php echo $form->error($customers,'customer_name'); ?>


It is showing the available customers name from the customer table.But I need one thing more.That is it will show an addition link in dropdown called as Create One.Where the admin will click on this link and one lightbox will come with the create customer form where all the data entered will be save in the customer table.I am also uploading some images for reference.Any help and suggestions will be highly appreciable.Reference image has been uploaded here.

Does this help:

I think you wanting to add a item to your dropdownList that will not come from your model data.

You could add a static method to your model and then use that to create the dropdownList data.

Something like this:

public static function myDropDownData() {

  $staticData = array('add' => 'Create One');

  $modelData = CHtml::listData(Customers::model()->findAll(), 'id', 'customer_name');

  return array_merge($staticData, $modelData);


Then you change your view to something like this:

<?php echo $form->dropdownList($customers,'customer_name', Customers::myDropDownData(), array('empty'=>'Choose one')); ?>

I hope that helps.

Thanks for your reply.I checked the link but the documentation written here is too complex and it is very hard to understand.Can you please describe it in a simple.I am really new to this framework and really need help.Waiting for any reply…

Well I’ve personally found that wiki very detailed. If you look closely, you see it’s rather complete than complex, and the modifications to your existing CRUD (gii) code are minor.

Anyway, maybe you should dare dive into that wiki, and if there’s anything unclear, you’ll find many members happy to help (here or on the wiki page).

Yes,I made dive into the docs and made necessary changes.But I got that the create Customer link which should come in one of the option in drop down list is coming just side by the drop down option.The code is something like this

    <div class="row">

      <?php echo $form->labelEx($customers,'customer_name'); ?>

      <div id="job">

      <?php echo $form->dropDownList($customers,'customer_name',CHtml::listData(Customers::model()->findAll(),'id','customer_name'),array('prompt'=>'Select')); ?>

      <?php echo CHtml::ajaxLink(Yii::t('customers','Create customers'),$this->createUrl('customers/create'),array(

      'onclick'=>'$("#customers").dialog("open"); return false;',



    <div id="jobDialog"></div>



The nature of the HTML select tag doesn’t allow to put links directly in it. Instead you should add a “New Client” value/pair in the select, and handle the select’s change event in jQuery. When the value is that of “New Client”, you fire the ajax.

Something along these lines:

<div class="row">

  <?php echo $form->labelEx($customers,'customer_name'); ?>

  <?php echo $form->dropdownList(



// I got the following line syntax from

                                array('someUniqueValue' => 'New Client') + CHtml::listData(Customers::model()->findAll(), 'id', 'customer_name'),

                                array('empty' => Yii::t('customers', 'Choose one'))

                            ); ?>

  <div id="newClientDialog"></div>


The jQuery


	$('#Customers_customer_name').change(function(e){ // please check that it's the correct select's Id in your HTML source

		if($(this).val() == 'someUniqueValue') {










			return false; // or e.preventDefault();




And then, your addNew action in your Customer controller would look like in the wiki. And ‘jobDialog’ would be replaced by ‘newClientDialog’.

If you’re still stuck, please get back to us with what you would have tried.

Thanks @ bennouna,for your quick reply.

I tried the way that you have given instruction above but its not working at all.I have added the jQuery and have made changes to the Customer controller file as per link but its not working at all.Can you give me some idea about that?I really need this.Any help and suggestion will be highly appreciable.

Well thanks @NaX.It helped me.But I want in Choose one I want to make a link to the customers/create link.When someone will click on the choose one it will open the customers create page.

I though you wanted to bind a JQuery Ajax call to the select option ‘Create One’ that would open a dialog or something. I got the idea form the Drupal Hierarchical Select module. See Places I’ve visited on Taxonomy demo here:

thanks very much NaX

people like you keeps the web moving. your code helped me a lot. i was trying to implement common class for dropdown list with datas from DB. I tried many finally your code worked.