Load View With Fancybox

Hi

I created app with yii and i needed to load view form in Fancy-box so i did that but my problem when i clicked on submit button , from redirected me to controller action without validated form . How to validate form with out redirect , mean validate inside Fancy-box ?

view :





<?php


$config = array( 


);





$this->widget('application.extensions.fancybox.EFancyBox', array(

'target'=>'#getaction',

'config'=>$config,));


echo CHtml::link('Add Section',array('section/create'),array('id'=>'getaction'));

?>










FormView _from from call from another view


   




<div class="form">


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

	'id'=>'section-form',

	'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,'name'); ?>

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

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

	</div>


	<div class="row buttons">

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

	</div>


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


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










Controller





public function actionCreate()

{

	$model=new Section;








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

	{

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

    	if($model->validate())

    	//// Do Som code here 

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

	}


	$this->render('create',array(

    	'model'=>$model,

	));




}










Thanks in advance :rolleyes:

You have to respond to the ajax call in the controller to do the ajax validation.

You can see the examples in Gii-generated actionCreate and actionUpdate.




public function actionCreate()

{

	$model=new Section;


	if(isset($_POST['ajax']) && $_POST['ajax']==='section-form')

	{

		echo CActiveForm::validate($model);

		Yii::app()->end();

	}


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

	{

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

        	if($model->validate())

    	        //// Do Som code here 

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

	}


	$this->render('create',array(

        	'model'=>$model,

	));


}



Thank u my bro , but not work also redirect me to view when clicked submit button .

I need to valdiate inside fancybox .

Thanks in Advance

Dear Friend

kindly check whether the following changes are helpful.




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

        'id'=>'section-form',

        'enableAjaxValidation'=>true,

        'clientOptions'=>array(

             validateOnSubmit'=>true,

            ),

)); ?>








public function actionCreate()

{

        $model=new Section;


        $this->performAjaxValidation($model); //This is the added line.


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

        {

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

        if($model->validate())

        //// Do Som code here 

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

        }


        $this->render('create',array(

        'model'=>$model,

        ));


}




Also check in your console for javascript errors.

Regards.

I guess i need to add ajaxbuuton ??!!

No, I don’t think so.

How do you render your form in fancy box?

I called it by

$this->widget(‘application.extensions.fancybox.EFancyBox’, array( ‘target’=>’#getaction’, ‘config’=>$config,)); echo CHtml::link(‘Add Section’,array(‘section/create’),array(‘id’=>‘getaction’));

then controller i tested twice ways :

1- render

2- renderpartial

also not work and redirect me :(

Check http://www.yiiframework.com/extension/quickdlgs

I’m sorry but I’m not familiar with the fancybox, so I don’t know how to render CActiveForm correctly in it. It looks like the script for the active form is not in effect.

Did you check the script in your developer tool of the browser?

Don’t worry ,Thank you softrak

Dear Friend

The following is working in my localhost.

Kindly check it.

_form.php




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

	'id'=>'section-form',

	'enableAjaxValidation'=>true,

	//'enableClientValidation'=>true,

	'clientOptions'=>array('validateOnSubmit'=>true), //This is very important

)); ?>


	<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,'name'); ?>

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

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

	</div>


	<div class="row buttons">

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

	</div>


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



In controller




ublic function actionCreate()

	{

		$model=new Section;


		// Uncomment the following line if AJAX validation is needed

		 $this->performAjaxValidation($model);//You have enabled ajax validation. You have to uncomment this line.


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

		{

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

			if($model->save())

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

		}


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

          echo $this->renderPartial('_form',array('model'=>$model),true,true);//This will bring out the view along with its script.

          

		else $this->render('create',array(

			'model'=>$model,

		));

	}



Regards.

3633

fancybox.png

Thank you seenivasan so much you fix my problem :D

Hi,

Just add Yii::app()->clientScript->scriptMap[’*.js’] = false; before renderPartial .

Best Regards,

Ankit Modi

@seenivasan: THANK YOU! Dude, you’re awesome!! I’ve been looking everywhere for this. You’re my saviour!!!

While the above is definitely working for me, I found something that can break it. If I want to suppress errors and remove:




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



then it breaks. When I submit the page, it’s not rendered in as a normal page instead of inside EFancyBox. Bizarrely, this is only happening when I remove the “echo $form->error()” for the “email” attribute.