Cjuiautocomplet Not Submitting Value

I looked at pretty much all relevent posts on this matter but can’t determine why the value, selected from the autocomplete list, is not submitted with the form? The other annoying thing is that if there is a validation error on another field in the form the value from my autocomplete field is blanked out (suspect this is related to the first problem).

Just to clarify, I’m trying to submit both the, ID of the autocomplete value, as well as the advertiserName(list value). The id is put in a hidden field and works fine…but the advertiserName is blank when submitted. Also the alerts show correct values.

InsertController code :


	

public function actionAutocomplete()

	{

			if(!Yii::app()->request->isAjaxRequest)

			   throw new CHttpException(400,Yii::t('app','Invalid request. Please do not repeat this request again.'));

			

			$term=$_GET['term']; 

			

			$criteria=new CDbCriteria;

			$criteria->compare('advertiserName',$term,true);

			$criteria->order='id DESC';

			$criteria->limit=10;

			

			$models=Advertiser::model()->findAll($criteria);

			$returnArray=array();

			foreach($models AS $model)

			{

					$returnArray[]=array(

							'label'=>CHtml::encode($model->advertiserName),

							'value'=>CHtml::encode($model->advertiserName),

							'id'=>(int)$model->id,

					);

			}

			echo CJSON::encode($returnArray);

			Yii::app()->end();

	}



View:




		<?php echo $form->hiddenField($model,'advertiserId'); ?>

		<?php echo $form->labelEx($model,'advertiserName'); ?>

		<?php

		$this->widget('zii.widgets.jui.CJuiAutoComplete', array(

				'name'=>'Insert_advertiserName',

				'sourceUrl'=>Yii::app()->createUrl('advertiser/autocomplete'),

				'options'=>array(

						'minLength'=>'2',

						'select'=>'js:function(event, ui) {

							$("#Insert_advertiserId").val(ui.item.id);

							alert(ui.item.label);

							value = $("#Insert_advertiserName").val();

							alert("field:" + value);

						}',


				),

				'htmlOptions'=>array('class'=>'field select full','style'=>'height:20px;margin-top:5px')

		));

		?>

		<?php echo $form->error($model,'advertiserName'); ?>




I would appreciate any suggestions.

In your Insert form model you don’t have advertiserName among list of field passed to save validator (last one in rules() function).

Add it there and see, if this fixes problem.

This is a common mistake (I can’t count how many time I did it! :]). Yii passes field values automatically only for fields listed in required and safe validators list. So, if your advertiserName isn’t among required fields, must be added to safe validator.

Thanks for the info, but it didn’t make any difference - I did at one point have it in the required array. Here’s the Javascript that the widget generates and also the sql error message I get.




jQuery(function($) {

jQuery('#Insert_insertDate').datepicker({'showAnim':'fold','dateFormat':'yy-mm-dd'});

jQuery('#Insert_advertiserName').autocomplete({'minLength':'2','select':function(event, ui) {

							$("#Insert_advertiserId").val(ui.item.id);

							alert(ui.item.label);

							value = $("#Insert_advertiserName").val();

							alert("field:" + value);

						},'close':function(){

						   

						},'source':'/ims/index.php/advertiser/autocomplete'});

});




 General error: 1364 Field 'advertiserName' doesn't have a default value. The SQL statement executed was: INSERT INTO `insert` (`marketId`, `productId`, `insertDate`, `zoneId`, `zip`, `formatId`, `pageCount`, `advertiserId`, `categoryId`, `weight`, `rateId`) VALUES (:yp0, :yp1, :yp2, :yp3, :yp4, :yp5, :yp6, :yp7, :yp8, :yp9, :yp10)

You say, that this is code generated by widget and you haven’t modified it in anyway? You say, that Yii widget generates JavaScript code with alerts inside? :] I wouldn’t agree, but maybe there is something I don’t know! :]

But seriously. I have never played with CJuiAutocomplete, so I can’t tell you, if this is normal behaviour (that only ID is passed by, not actual value selected) or if there is an error in your code or somewhere else, that blocks sending value from autocomplete, that would normally get sent.

What I would suggest:

[list=1][]Add another hidden field to your view and also bind jQuery change event on that autocomplete field. Store its value in that hidden extra field, add it to validators, to safe, so its value will be passed, and check, if you can read value this way?[]Put yourself a jQuery UI Autocomplete widget, without using Yii wrapper, make its name so, its value and id are passed along with other fields in your form and check, if this works.[/list]

And as for your second problem in first post – AFAIK, when there is a validation error, Yii should handle it completely by itself (either at client-side, in JavaScript, or at sever-side, after page reload). In this situation, not only your autocomplete field, but all of values from all your fields shouldn’t be sent to the server.

[size=“1”]A small notice. I’m tired and drunk! :] There is a high chance, that I just wrote you a complete non-sense! :] Sorry…[/size]

What I meant was the Widget is translated into Jquery, the alerts were part of the javascript I entered in the widget for testing.

I’ll try some of your suggestions, but thinking this is not a practical solution for what I want. I originally used a drop-down which worked fine, but the user wants to add names on the fly…thought I could use the autocomplete field and some how recognize when it was a new name and add it to the table.

Thanks for your help.