Yii Framework Forum: Campo Com Autocomplete - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Campo Com Autocomplete Rate Topic: -----

#1 User is offline   bia 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • Joined: 17-January 13

Posted 04 April 2013 - 12:44 PM

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;
       }
    }

"Só sei que nada sei..."
0

#2 User is offline   obscuremind 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 14-December 12

Posted 04 April 2013 - 07:26 PM

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.'%');

0

#3 User is offline   Newerton 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 820
  • Joined: 27-April 10
  • Location:Campo Grande/MS - Cambé/PR, Brasil

Posted 05 April 2013 - 08:16 PM

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.

This post has been edited by Newerton: 05 April 2013 - 08:16 PM

Newerton Vargas de Araújo
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users