Yii Framework Forum: Form validation redirect - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Form validation redirect Rate Topic: -----

#1 User is offline   tuga 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 54
  • Joined: 02-January 10

Posted 01 March 2010 - 11:32 AM

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
0

#2 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 01 March 2010 - 11:38 AM

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

#3 User is offline   bettor 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 747
  • Joined: 02-February 09

Posted 01 March 2010 - 12:05 PM

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...
0

#4 User is offline   tuga 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 54
  • Joined: 02-January 10

Posted 01 March 2010 - 05:15 PM

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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users