mirror of
				https://github.com/yiisoft/yii2.git
				synced 2025-11-04 14:46:19 +08:00 
			
		
		
		
	Fixes #2458: Added missing validaton to advanced app forms, separated validation from email sending errors for contact form
This commit is contained in:
		@ -87,8 +87,12 @@ class SiteController extends Controller
 | 
				
			|||||||
	public function actionContact()
 | 
						public function actionContact()
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		$model = new ContactForm();
 | 
							$model = new ContactForm();
 | 
				
			||||||
		if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
 | 
							if ($model->load(Yii::$app->request->post()) && $model->validate()) {
 | 
				
			||||||
			Yii::$app->session->setFlash('success', 'Thank you for contacting us. We will respond to you as soon as possible.');
 | 
								if ($model->sendEmail(Yii::$app->params['adminEmail'])) {
 | 
				
			||||||
 | 
									Yii::$app->session->setFlash('success', 'Thank you for contacting us. We will respond to you as soon as possible.');
 | 
				
			||||||
 | 
								} else {
 | 
				
			||||||
 | 
									Yii::$app->session->setFlash('error', 'There was an error sending email.');
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
			return $this->refresh();
 | 
								return $this->refresh();
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
			return $this->render('contact', [
 | 
								return $this->render('contact', [
 | 
				
			||||||
@ -122,7 +126,7 @@ class SiteController extends Controller
 | 
				
			|||||||
	public function actionRequestPasswordReset()
 | 
						public function actionRequestPasswordReset()
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		$model = new PasswordResetRequestForm();
 | 
							$model = new PasswordResetRequestForm();
 | 
				
			||||||
		if ($model->load(Yii::$app->request->post())) {
 | 
							if ($model->load(Yii::$app->request->post()) && $model->validate()) {
 | 
				
			||||||
			if ($model->sendEmail()) {
 | 
								if ($model->sendEmail()) {
 | 
				
			||||||
				Yii::$app->getSession()->setFlash('success', 'Check your email for further instructions.');
 | 
									Yii::$app->getSession()->setFlash('success', 'Check your email for further instructions.');
 | 
				
			||||||
				return $this->goHome();
 | 
									return $this->goHome();
 | 
				
			||||||
@ -144,7 +148,7 @@ class SiteController extends Controller
 | 
				
			|||||||
			throw new BadRequestHttpException($e->getMessage());
 | 
								throw new BadRequestHttpException($e->getMessage());
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if ($model->load(Yii::$app->request->post()) && $model->resetPassword()) {
 | 
							if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword()) {
 | 
				
			||||||
			Yii::$app->getSession()->setFlash('success', 'New password was saved.');
 | 
								Yii::$app->getSession()->setFlash('success', 'New password was saved.');
 | 
				
			||||||
			return $this->goHome();
 | 
								return $this->goHome();
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
				
			|||||||
@ -45,20 +45,15 @@ class ContactForm extends Model
 | 
				
			|||||||
	 * Sends an email to the specified email address using the information collected by this model.
 | 
						 * Sends an email to the specified email address using the information collected by this model.
 | 
				
			||||||
	 *
 | 
						 *
 | 
				
			||||||
	 * @param string $email the target email address
 | 
						 * @param string $email the target email address
 | 
				
			||||||
	 * @return boolean whether the model passes validation
 | 
						 * @return boolean whether the email was sent
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public function contact($email)
 | 
						public function sendEmail($email)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		if ($this->validate()) {
 | 
							return Yii::$app->mail->compose()
 | 
				
			||||||
			Yii::$app->mail->compose()
 | 
								->setTo($email)
 | 
				
			||||||
				->setTo($email)
 | 
								->setFrom([$this->email => $this->name])
 | 
				
			||||||
				->setFrom([$this->email => $this->name])
 | 
								->setSubject($this->subject)
 | 
				
			||||||
				->setSubject($this->subject)
 | 
								->setTextBody($this->body)
 | 
				
			||||||
				->setTextBody($this->body)
 | 
								->send();
 | 
				
			||||||
				->send();
 | 
					 | 
				
			||||||
			return true;
 | 
					 | 
				
			||||||
		} else {
 | 
					 | 
				
			||||||
			return false;
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -37,17 +37,15 @@ class PasswordResetRequestForm extends Model
 | 
				
			|||||||
			'email' => $this->email,
 | 
								'email' => $this->email,
 | 
				
			||||||
		]);
 | 
							]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if (!$user) {
 | 
							if ($user) {
 | 
				
			||||||
			return false;
 | 
								$user->generatePasswordResetToken();
 | 
				
			||||||
		}
 | 
								if ($user->save()) {
 | 
				
			||||||
 | 
									return \Yii::$app->mail->compose('passwordResetToken', ['user' => $user])
 | 
				
			||||||
		$user->generatePasswordResetToken();
 | 
										->setFrom([\Yii::$app->params['supportEmail'] => \Yii::$app->name . ' robot'])
 | 
				
			||||||
		if ($user->save()) {
 | 
										->setTo($this->email)
 | 
				
			||||||
			return \Yii::$app->mail->compose('passwordResetToken', ['user' => $user])
 | 
										->setSubject('Password reset for ' . \Yii::$app->name)
 | 
				
			||||||
				->setFrom([\Yii::$app->params['supportEmail'] => \Yii::$app->name . ' robot'])
 | 
										->send();
 | 
				
			||||||
				->setTo($this->email)
 | 
								}
 | 
				
			||||||
				->setSubject('Password reset for ' . \Yii::$app->name)
 | 
					 | 
				
			||||||
				->send();
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		return false;
 | 
							return false;
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user