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