mirror of
				https://github.com/yiisoft/yii2.git
				synced 2025-11-04 06:37:55 +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()
 | 
			
		||||
	{
 | 
			
		||||
		$model = new ContactForm();
 | 
			
		||||
		if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
 | 
			
		||||
			Yii::$app->session->setFlash('success', 'Thank you for contacting us. We will respond to you as soon as possible.');
 | 
			
		||||
		if ($model->load(Yii::$app->request->post()) && $model->validate()) {
 | 
			
		||||
			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();
 | 
			
		||||
		} else {
 | 
			
		||||
			return $this->render('contact', [
 | 
			
		||||
@ -122,7 +126,7 @@ class SiteController extends Controller
 | 
			
		||||
	public function actionRequestPasswordReset()
 | 
			
		||||
	{
 | 
			
		||||
		$model = new PasswordResetRequestForm();
 | 
			
		||||
		if ($model->load(Yii::$app->request->post())) {
 | 
			
		||||
		if ($model->load(Yii::$app->request->post()) && $model->validate()) {
 | 
			
		||||
			if ($model->sendEmail()) {
 | 
			
		||||
				Yii::$app->getSession()->setFlash('success', 'Check your email for further instructions.');
 | 
			
		||||
				return $this->goHome();
 | 
			
		||||
@ -144,7 +148,7 @@ class SiteController extends Controller
 | 
			
		||||
			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.');
 | 
			
		||||
			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.
 | 
			
		||||
	 *
 | 
			
		||||
	 * @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()) {
 | 
			
		||||
			Yii::$app->mail->compose()
 | 
			
		||||
				->setTo($email)
 | 
			
		||||
				->setFrom([$this->email => $this->name])
 | 
			
		||||
				->setSubject($this->subject)
 | 
			
		||||
				->setTextBody($this->body)
 | 
			
		||||
				->send();
 | 
			
		||||
			return true;
 | 
			
		||||
		} else {
 | 
			
		||||
			return false;
 | 
			
		||||
		}
 | 
			
		||||
		return Yii::$app->mail->compose()
 | 
			
		||||
			->setTo($email)
 | 
			
		||||
			->setFrom([$this->email => $this->name])
 | 
			
		||||
			->setSubject($this->subject)
 | 
			
		||||
			->setTextBody($this->body)
 | 
			
		||||
			->send();
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -37,17 +37,15 @@ class PasswordResetRequestForm extends Model
 | 
			
		||||
			'email' => $this->email,
 | 
			
		||||
		]);
 | 
			
		||||
 | 
			
		||||
		if (!$user) {
 | 
			
		||||
			return false;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		$user->generatePasswordResetToken();
 | 
			
		||||
		if ($user->save()) {
 | 
			
		||||
			return \Yii::$app->mail->compose('passwordResetToken', ['user' => $user])
 | 
			
		||||
				->setFrom([\Yii::$app->params['supportEmail'] => \Yii::$app->name . ' robot'])
 | 
			
		||||
				->setTo($this->email)
 | 
			
		||||
				->setSubject('Password reset for ' . \Yii::$app->name)
 | 
			
		||||
				->send();
 | 
			
		||||
		if ($user) {
 | 
			
		||||
			$user->generatePasswordResetToken();
 | 
			
		||||
			if ($user->save()) {
 | 
			
		||||
				return \Yii::$app->mail->compose('passwordResetToken', ['user' => $user])
 | 
			
		||||
					->setFrom([\Yii::$app->params['supportEmail'] => \Yii::$app->name . ' robot'])
 | 
			
		||||
					->setTo($this->email)
 | 
			
		||||
					->setSubject('Password reset for ' . \Yii::$app->name)
 | 
			
		||||
					->send();
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return false;
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user