Form validation redirect

Hi all!

I’m trying to make a simple contact form, what I want is that when someone sends a email, after successful validation, the user stays on the same page, and not redirected. I tried the following code, but after the validation the fields of the form are filled with previous input values.


public function actionContacts(){

		$mdlContact = new ContactForm();

		if (isset($_POST['ContactForm'])) {

			$mdlContact->attributes = $_POST['ContactForm'];

			if ($mdlContact->validate()){

				Yii::app()->user->setFlash('success',"Email sent!");

			}

				

		}

		

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

			'mdlContact'=>$mdlContact));

	}

So I tried this


public function actionContacts(){

		$mdlContact = new ContactForm();

		if (isset($_POST['ContactForm'])) {

			$mdlContact->attributes = $_POST['ContactForm'];

			if ($mdlContact->validate()){

				Yii::app()->user->setFlash('success',"Email sent!");

]$this->redirect('contacts');//Here I redirect for the same action

			}

				

		}

		

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

			'mdlContact'=>$mdlContact));

	}

And it works but doesn’t seem very logical or the correct way of doing it, can someone please advise me, how to achieve this.

Thanks

You can also use $this->refresh().

the only thing that does not seem logical is that you perform the redirection before you send the email to yourself…other than that I don’t see anything wrong…please explain why you are upset…

Thank you Mike and bettor.

@Mike

I indeed changed the code to $this->refresh(), it seems more logical.

@bettor

I thought I was doing something wrong, as I am a noobie in Yii I thought I could be missing something.

In fact the code for the email part is missing.

Once again thank you both for your help.