S
S
Svetlana Galenko2019-12-11 10:36:50
This
Svetlana Galenko, 2019-12-11 10:36:50

The contact form doesn't work, instead it's Call to a member function formName() on null?

Please help me to deal with the error, I have already broken my head ... I don’t understand what the problem could be ..
I have a contact form:

<?php $form = ActiveForm::begin(['method' => 'POST', 'fieldConfig' => [
                            'labelOptions' => [
                                'class' => 'wakeup__form-name',
                            ]
                        ]]); ?>

                        <div class="wakeup__form-group">
                            <?= $form->field($model, 'name', ['inputOptions' => ['class' => 'wakeup__form-field', 'id' => 'code']])->label("$model->getAttributeLabel('Код')") ?>
<!--                            <div class="wakeup__form-error">Неверный формат промокода</div>-->
                        </div>

                        <div class="wakeup__form-group">
                            <?= $form->field($model, 'phone', ['inputOptions' => ['class' => 'wakeup__form-field', 'id' => 'phone']])->label("$model->getAttributeLabel('+998(9X)XXX-XX-XX')") ?>
<!--                            <div class="wakeup__form-error">Неверный формат телефона</div>-->
                        </div>

                        <div class="wakeup__form-check-wrap">
                            <label class="wakeup__form-check" for="agreement">
                                <input placeholder="Код" class="wakeup__form-check-input" type="checkbox" id="agreement" name="agreement">
                                <span class="wakeup__form-check-placeholder"></span>
                                Согласен с <a href="/images/Usloviya_polozhenie_viktorini.pdf" download="Условия_и_Положения_Викторины.pdf" target="_blank">правилами Викторины</a>
                            </label>
                        </div>

                        <label class="wakeup__form-btn disabled" for="wake-up-submit">
                            Активировать услугу
                            <button id="wake-up-submit" hidden></button>
                        </label>
                        <?php ActiveForm::end(); ?>

                        <?php endif; ?>

Controller:
public function actionIndex()
    {
        $model = new AppointmentForm();
        if ($model->load(Yii::$app->request->post()) && $model->appointment(Yii::$app->params['adminEmail'])) {
            Yii::$app->session->setFlash('contactFormSubmitted');
            return $this->refresh();
        }  else {
            return $this->render('index', ['model'=> $model,]);
        }
    }

Model:
<?php

namespace app\models;

use Yii;
use yii\db\ActiveRecord;

/**
 * This is the model class for table "system_news".
 *
 * @property string $name
 * @property string $phone
 */
class AppointmentForm extends ActiveRecord
{
    public $name;
    public $phone;

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'system_appointment';
    }
    /**
     * @return array the validation rules.
     */
    public function rules()
    {
        return [
            // name, email, subject and body are required
            [['name', 'phone'], 'required'],
        ];
    }

    /**
     * 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 model passes validation
     */
    public function appointment($name)
    {
        $content = "<p>Name: " . $this->name . "</p>";
        $content .= "<p>Phone: " . $this->phone . "</p>";
        if ($this->validate()) {
            Yii::$app->mailer->compose([
                "@app/mail/layouts/html","content" => $content
            ])
                //->setTo($email)
                ->setTo('[email protected]')
                ->setFrom([\Yii::$app->params['supportEmail'] => $this->name])
                ->setTextBody("<p>Имя: " . $this->name . ";</p>"
                    . "<p>\nТелефон: " . $this->phone . ";</p>")
                ->send();

            return true;
        }
        return false;
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question