octicon-rss(16/)
You've already forked yii2
mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-10 02:13:17 +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:
octicon-git-branch(16/)
octicon-tag(16/)
octicon-diff(16/tw-mr-1) 3 changed files with 25 additions and 28 deletions
@@ -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
|
|||||||
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,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
|
|||||||
->setSubject('Password reset for ' . \Yii::$app->name)
|
->setSubject('Password reset for ' . \Yii::$app->name)
|
||||||
->send();
|
->send();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user