Activeform Autocomplete

Could not find in forums how to add autocomplete text field to TbActiveForm (which is bootstrap widget for CActiveForm), so here it is:

In form view, register jquery ui js and css first:




		jQuery("#' . CHtml::activeId($model, "username") . '").autocomplete({"showAnim":"fold","source":"/user/ajaxGetUserList"});


Form field is added as usual:

<?php echo $form->textFieldRow($model, 'username', array('class'=>'span2','maxlength'=>255));?>

And in controller:

public function actionAjaxGetUserList(){

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

			$data = array();

			if ( isset( $_GET['term'] ) ) {

				$qtxt ="SELECT username FROM " . User::model()->tableSchema->name . " WHERE username LIKE :match LIMIT 10";

				$command =Yii::app()->db->createCommand( $qtxt );

				$command->bindValue( ":match", '%'.$_GET['term'].'%', PDO::PARAM_STR );

				$data =$command->queryColumn();


			echo CJSON::encode( $data );




for autocomplete

Right you can use autocomplete widget, but then form model validation errors won’t show up.