I have only recently begun working with Yii and so far I have managed to pick it up nicely, but now I am running into ‘problem’. Acually more a question on how to do things properly
Yii of course works with layout files where you can define the header, logo, footer, etc etc. Anything that you want to be visible troughout your application.
In my case, I want to add a single field CJuiAutoComplete widget within a form into the main.php layout file. This field is used to quicksearch for records in a persons database, I got this working and now rises the problem of doing something with the actually selected item. I do this as follows:
<div id="header_search-area">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'searchperson',
'action'=>'../persons/search',
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
));
$this->widget('zii.widgets.jui.CJuiAutoComplete',array(
'name'=>'search_person',
'value'=> $model->Persons,
'source'=>$this->createUrl('persons/searchperson'),
// additional javascript options for the autocomplete plugin
'options'=>array(
'minLength'=>'3',
'select'=>'js:function(event, ui) { console.log(ui.item.id +":"+ui.item.value); $("#searchperson").submit();}',
),
'htmlOptions'=>array(
'style'=>'height:20px; width: 250px;',
'id'=>'search_person',
'rel'=>'val',
),
));
echo $form->hiddenField($model,'person_id');
$this->endWidget();
?>
</div>
So I have created a Form ‘searchperson’ through the use of Gii and added the ‘search_person’ inputfield to it which has the autocomplete action attached to it. This function is currently setup so that on ‘select’ it will display the item that was selected and then it will submit the form, causing the user to be forwarded to the persons/search page.
However, because this is a single field form (for the eye) with just somebodies name in it, I need to create a hidden field where i can insert the persons ID upon select, so that this will end up in the POST data. Now, the proper way to do this of course would be the way I have currently added near the end of the form widget, this:
echo $form->hiddenField($model,'person_id');
However, because this form is not displayed in the ‘persons/search’ view, but rather in the main layout file my application has no idea what ‘$model’ is.
What is the best course of action to do now? The easiest way out would be to simply replace the CActiveForm’s hiddenField method with a manual html input field like this:
<input type="hidden" name="person_id" id="person_id" value="" />
In the CJuiAutoComplete widget i could then target this field, add the person_id returned from the search in it and then submit the form. Et voila, case closed.
But somehow I have the feeling this is not the best way to go about it. So what do I have to do here and how is the best way to handle this situation?