i have to models one is universities and the other is students what i want is in the student create form i want the university_id field to be using the auto complete function of yii and the auto complete should show the university name in the list and when user selects the university the id of that university should be used while submitting the form.
currently i have created an extra field to only get the list of the universities, in the firebug the data fetched is shown but the auto complete ul has list of blank list items. here is my code.
in the student create view(_form.php)
$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
'id'=>'university',
'name'=>'uni_id',
'value'=>$model->uni_id,
'source'=>$this->createUrl('Universities/getNames'),// <- path to controller which returns dynamic data
// additional javascript options for the autocomplete plugin
'options'=>array(
'showAnim'=>'fold',
'minLength'=>'3', // min chars to start search
'select'=>'js:function(event, ui) { console.log(ui.item.id +":"+ui.item.value); }'// i don't know what this does actually
),
));
in the universities controller
public function actiongetNames(){
if (!empty($_GET['term'])) {
$sql = 'SELECT id, name FROM universities WHERE name LIKE :qterm ';
$sql .= ' ORDER BY name ASC';
$command = Yii::app()->db->createCommand($sql);
$qterm = $_GET['term'].'%';
$command->bindParam(":qterm", $qterm, PDO::PARAM_STR);
$models = $command->queryAll();
$arr = array();
foreach ($models as $model)
{
$arr[] = array(
'id'=>$model['id'],
'name'=>$model['name'],
);
}
echo CJSON::encode($models); exit;
} else {
return false;
}
}
after searching the forum i have build up this code, now i m stuck and cann’t figure out what could possibly go wrong?
i fixed the method name and also checked the term parameter the result returned is shown in the firebug on entering the term "stan" in the auto complete field. but not in the list actually no list appears.
output in firebug:
[{"id":"15808","name":"Stanford University"},{"id":"15809","name":"Stanly Community College"},{"id":"467","name":"Stanto Mariam University of Creative Technology"}]
i have hidden the actual field, first it works fine but if i select the autocomplete field list item and don’t fill any other field in the form and submit the form(don’t fill the other required fields) then the form is reloaded again after submission and the university field now contains the item id not the item text as i selected.
how can i store the uni name temporarily to show the user even if form has some errors and form reloads?
well fixed this issue too by creating another field in the unimember model, and database named as uni_name and used it as
if(isset($_POST['Unimembers']['uni_name'])){
$uni_name = $_POST['Unimembers']['uni_name'];
}else {
$uni_name = '';
}
$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
'id'=>'university',
'name'=>'uni_id',
'value'=>$uni_name,
'source'=>$this->createUrl('universities/getnames'),// <- path to controller which returns dynamic data
// additional javascript options for the autocomplete plugin
'options'=>array(
'showAnim'=>'fold',
'minLength'=>'3', // min chars to start search
'select'=>"js:function(event, ui) {
jQuery('#Unimembers_uni_id').val(ui.item.id);
jQuery('#Unimembers_uni_name').val(ui.item.value);}"
),
));
this resolves the issue but i want to know is this the correct method or any other way exists to solve it as the autocomplete field is not submitted in the post while submitting the form???
plus the auto complete field does not turn red if no value is added to it and form is submitted, how can i achieve this???