Cjuidialog Not Working

This is what my code looks like right now. As you can see I’m using tabs and CJuiDialog. The way the corde is right now, the Dialog actually comes up.


<?php

/* @var $this PeopleController */

/* @var $dataProvider CActiveDataProvider */


$this->breadcrumbs=array(

	'Peoples',

);

?>


<h1>People</h1>


<?php $this->beginWidget('system.web.widgets.CClipWidget', array('id'=>'Opciones')); ?>

    <?php echo CHtml::link('Create People', '#', array(

   'onclick'=>'$("#mydialog").dialog("open"); return false;',

));

?>


<?php 

$this->beginWidget('zii.widgets.jui.CJuiDialog',array(

    'id'=>'mydialog',

    // additional javascript options for the dialog plugin

    'options'=>array(

        'title'=>'Create People',

        'autoOpen'=>false,

        'modal'=>true,

    ),

));

?>

s

<?php $model = new People;

echo $this->renderPartial('_form', array('model'=>$model)); ?>


<?php $this->endWidget('zii.widgets.jui.CJuiDialog'); ?>


<script type="text/javascript">

function addPeople()

{

    <?php echo CHtml::ajax(array(

            'url'=>array('people/create'),

            'data'=> "js:$(this).serialize()",

            'type'=>'post',

            'dataType'=>'json',

            'success'=>"function(data)

            {

                if (data.status == 'failure')

                {

                    $('#dialogPeople div.divForForm').html(data.div);

                          // Here is the trick: on submit-> once again this function!

                    $('#dialogPeople div.divForForm form').submit(addPeople);

                }

                else

                {

                    $('#dialogPeople div.divForForm').html(data.div);

                    setTimeout(\"$('#dialogPeople').dialog('close') \",3000);

                }

 

            } ",

            ))?>;

    return false; 


}

</script>


<?php $this->endWidget(); ?>





<?php $this->beginWidget('system.web.widgets.CClipWidget', array('id'=>'Vista')); ?>

<?php $this->widget('zii.widgets.CListView', array(

	'dataProvider'=>$dataProvider,

	'itemView'=>'_view',

)); ?>




<?php $this->endWidget(); ?>




<?php

$tabParameters = array();

foreach($this->clips as $key=>$clip)

    $tabParameters['tab'.(count($tabParameters)+1)] = array('title'=>$key, 'content'=>$clip);

?>

 

<?php $this->widget('system.web.widgets.CTabView', array('tabs'=>$tabParameters)); ?>

The first problem is if i remove the following line:


$model = new People;

which i took from actionCreate in the controller, then the page won’t load because of this line:


echo $this->renderPartial('_form', array('model'=>$model))

saying that model is an undefined variable…

But what’s more, by leaving the code the way it is, it just won’t save records to the database… And the worst part is I can’t even check if it’s trying to do the job because when I set the rules to validate, it also crashes and quotes some lines from yii’s CValidator.php

Ok, so this is what I’ve managed to do so far.

For the Create Controller


public function actionCreate()

	{

$model=new People;

 

        // Uncomment the following line if AJAX validation is needed

        $this->performAjaxValidation($model);

 

        if(isset($_POST['People']))

        {

            $model->attributes=$_POST['People'];

            if($model->save())

            {

                if (Yii::app()->request->isAjaxRequest)

                {

                    echo CJSON::encode(array(

                        'status'=>'success', 

                        'div'=>'People successfully added'

                        ));

                    exit;               

                }

                else

                    $this->redirect(array('view','id'=>$model->id));

            }

        }

 

        if (Yii::app()->request->isAjaxRequest)

        {

            echo CJSON::encode(array(

                'status'=>'failure', 

                'div'=>$this->renderPartial('_form', array('model'=>$model), true, true)));

            exit;               

        }

        else

            $this->render('create',array('model'=>$model,));


		        if (Yii::app()->request->isAjaxRequest)

		{                

		   Yii::app()->clientScript->scriptMap['jquery.js'] = false;   

		   echo CJSON::encode(array(

		      'status'=>'failure', 

		      'div'=>$this->renderPartial('_form', array('model'=>$model), true, true)));

		       exit;               

		}

		else

                    $this->redirect(array('view','id'=>$model->id));


				if (Yii::app()->request->isAjaxRequest)

		{                

		   Yii::app()->clientScript->scriptMap['jquery.js'] = false;   

		   echo CJSON::encode(array(

		      'status'=>'success', 

		      'div'=>'People successfully added'));

		       exit;               

		}

		else

            $this->render('create',array('model'=>$model,));

    }

For the index


<?php echo CHtml::link('Create People', "",  // the link for open the dialog

    array(

        'style'=>'cursor: pointer; text-decoration: underline;',

        'onclick'=>"{addPeople(); $('#dialogPeople').dialog('open');}"));?>




<?php

$this->beginWidget('zii.widgets.jui.CJuiDialog', array( // the dialog

    'id'=>'dialogPeople',

    'options'=>array(

        'title'=>'Create People',

        'autoOpen'=>false,

        'modal'=>true,

        'width'=>550,

        'height'=>570,

    ),

));?>

<?php $model=new People;

 echo $this->renderPartial('_form', array('model'=>$model)); ?>

<?php $this->endWidget();?>

And the _form is pretty simple, the one that came with the crud generator


<div class="form">


<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'people-form',

	'enableClientValidation'=>false,

	'enableAjaxValidation'=>true,

)); ?>


	<p class="note">Fields with <span class="required">*</span> are required.</p>


	<?php echo $form->errorSummary($model); ?>


	<div class="row">

		<?php echo $form->labelEx($model,'cedula'); ?>

		<?php echo $form->textField($model,'cedula',array('size'=>8,'maxlength'=><img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='8)' />); ?>

		<?php echo $form->error($model,'cedula'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'nombre'); ?>

		<?php echo $form->textField($model,'nombre',array('size'=>60,'maxlength'=>255)); ?>

		<?php echo $form->error($model,'nombre'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'telefono'); ?>

		<?php echo $form->textField($model,'telefono',array('size'=>14,'maxlength'=>14)); ?>

		<?php echo $form->error($model,'telefono'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'email'); ?>

		<?php echo $form->textField($model,'email',array('size'=>60,'maxlength'=>255)); ?>

		<?php echo $form->error($model,'email'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'direccion'); ?>

		<?php echo $form->textArea($model,'direccion',array('rows'=>6, 'cols'=>50)); ?>

		<?php echo $form->error($model,'direccion'); ?>

	</div>


	<div class="row buttons">

		<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>

	</div>


<?php $this->endWidget(); ?>


</div><!-- form -->

Now, there’s a single piece of code that I’ve pasted in the controller, the model, index and _form, and it doesn’t really make a difference


<script type="text/javascript">

// here is the magic

function addPeople()

{

    <?php echo CHtml::ajax(array(

            'url'=>array('people/create'),

            'data'=> "js:$(this).serialize()",

            'type'=>'post',

            'dataType'=>'json',

            'success'=>"function(data)

            {

                if (data.status == 'failure')

                {

                    $('#dialogPeople div.divForForm').html(data.div);

                          // Here is the trick: on submit-> once again this function!

                    $('#dialogPeople div.divForForm form').submit(addPeople);

                }

                else

                {

                    $('#dialogPeople div.divForForm').html(data.div);

                    setTimeout(\"$('#dialogPeople').dialog('close') \",3000);

                }

 

            } ",

            ))?>;

    return false; 

 

}

 

</script>

So, no matter what I do, the CJuiDialog comes up, i enter the information, it validates with the simple validation rules that come with the model generator, since I haven’t got around to changing them yet.

However when i click the submit Button, the url just reloads without saving, when it’s supposed to stay and the dialog is supposed to show me a message saying my data was saved.

This is really bugging me because i haven’t even been able to advance to masking some fields and working on the rest of the project.

Some help, please?