2

I have done web application using Yii2 in that form submitted values is not saving in the database. I just used model+controller+view structure.Actually form is submitting in Mysql database but when i visited the database i can see only blank values for all the entries

SiteController.php

<?php

namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
use app\models\CanForm;
use app\models\UploadForm;

use yii\web\UploadedFile;

class SiteController extends Controller
{
    public function actionContact()
    {
        $model = new ContactForm();
        if ($model->load(Yii::$app->request->post())) {

          $model->name = $_POST['ContactForm']['name'];
          $model->email = $_POST['ContactForm']['email'];
          $model->subject = $_POST['ContactForm']['subject'];
          $model->body = $_POST['ContactForm']['body'];

           if ($model->save())
            Yii::$app->response->redirect(array('site/contact', 'model' => $model));
            return $this->refresh();
        } else {
            return $this->render('contact', [
                'model' => $model,
            ]);
        }
    }
}
}

Model:ContactForm.php


namespace app\models;

use Yii;
use yii\base\Model;

use yii\db\ActiveRecord;
/**
 * ContactForm is the model behind the contact form.
 */
class ContactForm extends \yii\db\ActiveRecord
{
    public $name;
    public $email;
    public $subject;
    public $body;
    public $verifyCode;

    /**
     * @return array the validation rules.
     */
    public function rules()
    {
        return [
            // name, email, subject and body are required
            [['name', 'email', 'subject', 'body'], 'required'],
            // email has to be a valid email address
            ['email', 'email'],
            // verifyCode needs to be entered correctly
            ['verifyCode', 'captcha'],
        ];
    }

    /**
     * @return array customized attribute labels
     */
    public function attributeLabels()
    {
        return [
            'verifyCode' => '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 boolean whether the model passes validation
     */
    public function contact($email)
    {
        if ($this->validate()) {
            Yii::$app->mailer->compose()
                ->setTo($email)
                ->setFrom([$this->email => $this->name])
                ->setSubject($this->subject)
                ->setTextBody($this->body)
                ->send();

            return true;
        } else {
            return false;
        }
    }
}
View:contact.php


    <div class="row">
        <div class="col-lg-5">
            <?php $form = ActiveForm::begin(['id' => 'contact-form']); ?>
                <?= $form->field($model, 'name') ?>
                <?= $form->field($model, 'email') ?>
                <?= $form->field($model, 'subject') ?>
                <?= $form->field($model, 'body')->textArea(['rows' => 6]) ?>
                <?= $form->field($model, 'verifyCode')->widget(Captcha::className(), [
                    'template' => '<div class="row"><div class="col-lg-3">{image}</div><div class="col-lg-6">{input}</div></div>',
                ]) ?>
                <div class="form-group">
                    <?= Html::submitButton('Submit', ['class' => 'btn btn-primary', 'name' => 'contact-button']) ?>
                </div>
            <?php ActiveForm::end(); ?>
2
  • Please add the code and more details. Commented Apr 24, 2015 at 5:17
  • How you can see blank values in database if the model didn't pass validation (that means it not saved)? Use load for massive assignment instead of manual processing like that. Commented Apr 24, 2015 at 6:07

1 Answer 1

1

From the code you have given it seems you haven't bind your model with the database.

Add this function to your model:

public static function tableName()
{
    return 'your_table_name';
}

Also you don't have to manually assign each value to the model. Massive assignment does that for you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.