Commit 036a811e by Alexander Makarov

Fixes #2458: Added missing validaton to advanced app forms, separated validation…

Fixes #2458: Added missing validaton to advanced app forms, separated validation from email sending errors for contact form
parent 7660827d
...@@ -87,8 +87,12 @@ class SiteController extends Controller ...@@ -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()) {
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.'); 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 ...@@ -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 ...@@ -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 ...@@ -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,10 +37,7 @@ class PasswordResetRequestForm extends Model ...@@ -37,10 +37,7 @@ class PasswordResetRequestForm extends Model
'email' => $this->email, 'email' => $this->email,
]); ]);
if (!$user) { if ($user) {
return false;
}
$user->generatePasswordResetToken(); $user->generatePasswordResetToken();
if ($user->save()) { if ($user->save()) {
return \Yii::$app->mail->compose('passwordResetToken', ['user' => $user]) return \Yii::$app->mail->compose('passwordResetToken', ['user' => $user])
...@@ -49,6 +46,7 @@ class PasswordResetRequestForm extends Model ...@@ -49,6 +46,7 @@ class PasswordResetRequestForm extends Model
->setSubject('Password reset for ' . \Yii::$app->name) ->setSubject('Password reset for ' . \Yii::$app->name)
->send(); ->send();
} }
}
return false; return false;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment