Showing / Hiding form according to success / error

I’m trying go get the better way to manage form displaying / hiding when the form is still empty (or contains input errors) and when it has been validated.

Until now, my controller was:


public function actionRegister()

	{

		$model = new Model;

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

		{

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

			if($model->validate())

			{

				Yii::app()->user->setFlash('register', 'Yaaaaahooooo!! :-))');

				$this->refresh();

			}

		}

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

	}

And my view:


<?php

if(Yii::app()->user->hasFlash('register'))

{

?>

	<div class="flash-success">

		<?php echo Yii::app()->user->getFlash('register'); ?>

	</div>

<?php

}

else

{

?>

	<div class="form">

	<?php echo CHtml::beginForm(); ?>

	 

		<?php echo CHtml::errorSummary($model); ?>

		

			<div class="row">

				<?php echo CHtml::activeLabel($model, 'attribute'); ?>

				<?php echo CHtml::activeTextField($model, 'attribute') ?>

			</div>

		

		(...)

	 

		<div class="row submit">

			<?php echo CHtml::submitButton('OK'); ?>

		</div>

	 

	<?php echo CHtml::endForm(); ?>

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

<?php

}

?>

So, the presence of a "flash message" in my session was the wayI manage if displaying or not the form in the view. Was a not-too-bad solution… :slight_smile:

Now, I converted my form in a CActiveForm, and this system does not work anymore…

Here is my new controller code…


public function actionRegister()

	{

		$model = new Model;

		$this->performAjaxValidation($model);

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

		{

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

			if($model->validate())

			{

				Yii::app()->user->setFlash('register', 'Yaaaaahooooo!! :-))');

				$this->refresh();

			}

		}

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

	}

	

	protected function performAjaxValidation($model)

	{

		if(isset($_POST['ajax']) && $_POST['ajax'] === 'model-register')

		{

			echo CActiveForm::validate($model);

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

		}

	}

And my new view:


<?php

if(Yii::app()->user->hasFlash('register'))

{

?>

	<div class="flash-success">

		<?php echo Yii::app()->user->getFlash('register'); ?>

	</div>

<?php

}

else

{

?>

<div class="form">

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

	'id' => 'model-register',

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

			<?php echo $form->textField($model, 'attribute'); ?>

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

		</div>

		

		(...)


	<div class="row buttons">

		<?php echo CHtml::submitButton('OK'); ?>

	</div>

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

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

<?php

}

?>

I guess, the Yii::app()->end() function don’t let the “flash message” to be setted?

Or something else happens with that performAjaxValidation() and I must use another way to manage this "success message" being displayed and to validated form being not showed?

Thank for your help… :slight_smile:

I tried this on new webapp created with yiic, changed the siteController -> loginAction and the login.php, and it works for me…

Additionali you can try to use setState() instead of setFlash()…