I have created a form which displays correctly and the validation works in terms of required fields and captcha however when clicking submit nothing happens not fail message or success message and the email is not being sent. I have set it up similar to the default contact form.
I am unsure on what the problem is as no error are being thrown and I have compared it to the way the contact form is setup.
Review form model
<?php
namespace frontend\models;
use yii\base\Model;
use Yii;
/**
* ContactForm is the model behind the contact form.
*/
class SubmitReviewForm extends Model
{
public $name;
public $email;
public $title;
public $subtitle;
public $summary;
public $description;
public $legit;
public $fake;
public $brand;
public $department;
public $verifyCode;
/**
* Returns the validation rules for attributes.
*
* @return array
*/
public function rules()
{
return [
[['name', 'title', 'subtitle', 'summary', 'description', 'legit', 'fake', 'brand', 'department', 'verifyCode', 'email'], 'required'],
['email', 'email'],
['verifyCode', 'captcha'],
];
}
/**
* Returns the attribute labels.
*
* @return array
*/
public function attributeLabels()
{
return [
'name'=> Yii::t('app', 'Name'),
'email'=> Yii::t('app', 'Email'),
'title'=> Yii::t('app', 'Title'),
'subtitle' => Yii::t('app', 'Sub Title'),
'summary' => Yii::t('app', 'Summary'),
'description' => Yii::t('app', 'Description'),
'legit' => Yii::t('app', 'Legit'),
'fake'=> Yii::t('app', 'Fake'),
'brand'=> Yii::t('app', 'Brand'),
'department'=> Yii::t('app', 'Department'),
'verifyCode'=> Yii::t('app', 'Verification Code'),
];
}
/**
* Sends an email to the specified email address using the information
* collected by this model.
*
* @param string $email The target email address.
* @return bool Whether the email was sent.
*/
public function submitreview($email)
{
return Yii::$app->mailer->compose()
->setTo($email)
->setFrom([$this->email => $this->name])
->setSubject($this->title)
->setTextBody($this->description)
->send();
}
}
Controller
public function actionSubmitreview()
{
$this->layout = 'submitreview';
$model = new SubmitReviewForm();
if ($model->load(Yii::$app->request->post()) && $model->validate())
{
if ($model->submitreview(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('submitreview', [
'model' => $model,
]);
}
}
Aucun commentaire:
Enregistrer un commentaire