Campo Com Autocomplete

Pessoal estou fazendo um campo de código de Material com autocomplete, ele está buscando o

material correto de acordo com o código, mas quando vou submeter o formulário, dá um erro

dizendo que o código de Material está vazio, ou seja não está reconhecendo o código que inseri

no campo autocomplete, alguém sabe como resolver?

_form:




<div class="row">

        <?php echo $form->labelEx($model,'c&oacutedigo Material'); ?>

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

          array(

                         //name of the html field that will be generated

             'name'=>'codMaterial', 

			 'model' =>'codMaterial',

                         //replace controller/action with real ids

             'url'=>array('Material/FindAutores'), 

             'max'=>10, //specifies the max number of items to display

 

                         //specifies the number of chars that must be entered 

                         //before autocomplete initiates a lookup

             'minChars'=>1, 

             'delay'=>500, //number of milliseconds before lookup occurs

             'matchCase'=>false, //match case when performing a lookup?

 

                         //any additional html attributes that go inside of 

                         //the input field can be defined here

             'htmlOptions'=>array('size'=>'40'), 

 

             'methodChain'=>".result(function(event,item){\$(\"#codMaterial\").val(item[1]);})",

             ));

    ?>

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

Controller:




public function actionFindAutores()

    {

       if(Yii::app()->request->isAjaxRequest && isset($_GET['q']))

       {

            /* q is the default GET variable name that is used by

            / the autocomplete widget to pass in user input

            */

          $name = $_GET['q']; 

                    // this was set with the "max" attribute of the CAutoComplete widget

          $limit = min($_GET['limit'], 50); 

          $criteria = new CDbCriteria;

          $criteria->condition = "codMaterial LIKE :sterm";

          $criteria->params = array(':sterm' => '%'.$name.'%');

          $criteria->limit = $limit;


          $medicoArray = Material::model()->findAll($criteria);

          $returnVal = '';

          foreach($medicoArray as $medicoAccount)

          {

             $returnVal .= $medicoAccount->getAttribute('titulo').'|'

                                         .$medicoAccount->getAttribute('codMaterial')."\n";

          }

          echo $returnVal;

       }

    }




public function actionFindAutores()

    {

       if(Yii::app()->request->isAjaxRequest && isset($_GET['q']))

       {

            /* q is the default GET variable name that is used by

            / the autocomplete widget to pass in user input

            */

          $codMaterial = $_GET['q']; 

                    // this was set with the "max" attribute of the CAutoComplete widget

          $limit = min($_GET['limit'], 50); 

          $criteria = new CDbCriteria;

          $criteria->condition = "codMaterial LIKE :sterm";

           $criteria->params = array(":sterm"=>"%$codMaterial%");

          $criteria->limit = $limit;


          $medicoArray = Material::model()->findAll($criteria);

          $returnVal = '';

          foreach($medicoArray as $medicoAccount)

          {

             $returnVal .= $medicoAccount->getAttribute('titulo').'|'

                                         .$medicoAccount->getAttribute('codMaterial')."\n";

          }

          echo $returnVal;

       }

    }

2 erros encontrados.


$name = $_GET['q'];


$criteria->params = array(':sterm' => '%'.$name.'%');

Bia,

O problema está na hora de criar o campo. O nome do campo não está igual os demais.

Ele de ta criando <input/> com name="codMaterial", o correto seria name="SEU_MODEL[codMaterial]".

Então corrigi essa linha aqui:


'model' =>'codMaterial',

Para:


'model' =>$model,

E veja se resolve o problema.