CAutoComplete and CActiveForm

Hello.

Please help me to add CAutoComplete widget to CActivForm widget.

I have this code in my view file:


$form=$this->beginWidget('CActiveForm', array(

	'id'=>'vvid-form',

	'enableAjaxValidation'=>true,

)); ?>


	<?php echo $form->dropDownList($model,'predmet', $predmety, array('empty' => ''));?>

        <?php $this->widget('CAutoComplete',

          array(

             'model'=>$model,

             'name'=>'username', 

             .

             .

             .

             .

            ));

        ?>

         .

         .

         .

<?php $this->endWidget();?>



So form item with name ‘predmet’ is part of a $form and ‘username’ is not. How can I make ‘username’ part of $form?

Thanks.

Nobody is going to help me? :(

I don’t understand exactly your question - How can I make ‘username’ part of $form?

you mean part of $model ?

Take alook at the CAutoComplete cookbook - http://www.yiiframework.com/doc/cookbook/25/

so you get the idea behind the CAutoComplete

Let me explain what I need.

I have a form created with this code:


$form=$this->beginWidget('CActiveForm', array(

        'id'=>'vvid-form',

        'enableAjaxValidation'=>true,

));

This form must have two elements: dropdown list with name ‘predmet’ and text field with name ‘username’.

I include dropdown list into the form with this code:


<?php echo $form->dropDownList($model,'predmet', $predmety, array('empty' => ''));?>

Note $form-> that I use to include ‘predmet’ into my form.

The question is how to properly include into the form my textfield which is autocomplete widget.

Where $form-> must be in this code?


<?php $this->widget('CAutoComplete',

          array(

             'model'=>$model,

             'name'=>'username', 

             .

             .

             .

             .

            ));

?>

Simply don’t use $form, leave $this->widtget as it is.

But I will not be able to use validation. Or will I?

to use your thinking… validation is based on $model not on $form so as long as username is a property of the model it will be validated…

CAutoComplete is a widget and is used that way… it’s not part of the CActiveForm…

Yes, mdomba, you are right. Everything works Ok.

Tank you.

I solved my problem and here I want to make a little hint for those who want to use CAutoComplete widget within CActiveForm widget.

It’s very important to specify the right name property for your CAutocomplete widget. For example if your input name is ‘username’ like in my case, then instead of


'name'=>'username'

you have to write


'name'=>CHtml::activeName($model, 'username')