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
public function actionContact()
{
$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.');
} 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()
return Yii::$app->mail->compose()
->setTo($email)
->setFrom([$this->email => $this->name])
->setSubject($this->subject)
->setTextBody($this->body)
->send();
return true;
} else {
return false;
}
}
}
......@@ -37,10 +37,7 @@ class PasswordResetRequestForm extends Model
'email' => $this->email,
]);
if (!$user) {
return false;
}
if ($user) {
$user->generatePasswordResetToken();
if ($user->save()) {
return \Yii::$app->mail->compose('passwordResetToken', ['user' => $user])
......@@ -49,6 +46,7 @@ class PasswordResetRequestForm extends Model
->setSubject('Password reset for ' . \Yii::$app->name)
->send();
}
}
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