I have been trying to have a form rendered in a Cjuidialog. However the form will not render, the dialog is empty. To open the dialog i use the code below:
<?php echo CHtml::link('Add Project','',
array(
'style'=>'cursor: pointer; text-decoration: underline;',
'onclick'=>"{addWatchList(); $('#watchListDialog').dialog('open');}")); ?>
<?php
$this->beginWidget('zii.widgets.jui.CJuiDialog', array( // the dialog
'id'=>'watchListDialog',
'options'=>array(
'title'=>'Add Project',
'autoOpen'=>false,
'modal'=>true,
'width'=>550,
'height'=>470,
),
));?>
<div class="divForForm"></div>
<?php //<div id="watchListDialog"></div> ?>
<?php $this->endWidget();?>
<script type="text/javascript">
// here is the magic
function addWatchList()
{
<?php echo CHtml::ajax(array(
'url'=>array('/watchList/create'),
'data'=> "js:$(this).serialize()",
'type'=>'post',
'dataType'=>'json',
'success'=>"function(data)
{
if (data.status == 'failure')
{
$('#watchListDialog div.divForForm').html(data.div);
// Here is the trick: on submit-> once again this function!
$('#watchListDialog div.divForForm form').submit(addWatchList);
}
else
{
$('#watchListDialog div.divForForm').html(data.div);
setTimeout(\"$('#watchListDialog').dialog('close') \",3000);
}
} ",
)); ?>;
return false;
}
</script>
in the controller, the create action is
public function actionCreate()
{
$model = new WatchList;
//$this->performAjaxValidation($model);
// Flag to know if we will render the form or try to add new watchlist item
if(isset($_POST['WatchList']))
{
$model->attributes=$_POST['WatchList'];
$model->$user_id = Yii::app()->user->getID();
if($model->save()) {
if(Yii::app()->request->isAjaxRequest) {
echo CJSON::encode(array(
'status'=>'success',
'div'=>"Project successfully added"
));
exit;
}
}
}
if(Yii::app()->request->isAjaxRequest) {
echo CJSON::encode(array(
'status'=>'failure',
'div'=>$this->renderPartial('_create',array('model'=>$model))));
exit;
}
}
and the _create view file.
<?php $form=$this->beginWidget('bootstrap.widgets.TbActiveForm',array(
'id'=>'watchList-form',
'enableAjaxValidation'=>false,
)); ?>
<?php
echo CHtml::activeHiddenField($model, 'project_id');
echo $form->labelEx($model, 'project_id');
$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
'model' => $model,
'attritube' => 'project_id',
'sourceUrl' => array('/project/autocomplete'),
'options' => array(
'minLength' => 3,
'select' => new CJavaScriptExpression('function(event, ui) {
$("#\'.CHtml::activeId($model,\'project_id\').\'").val(ui.item.id);
return true;
}'),
),
'htmlOptions' => array(
'size' => 32,
'maxlength'=>32,
),
));
?>
<div class="form-actions">
<?php $this->widget('bootstrap.widgets.TbButton', array(
'buttonType'=>'submit',
'type'=>'primary',
'label'=>'Add',
)); ?>
</div>
<?php $this->endWidget(); ?>
any help would be appreciated.