Yii2:无法保存模型生成__csrf令牌

时间:2016-04-16 19:34:17

标签: yii yii2

控制器:

public function actionWriteus()
    {
        $model = new ContactUs();
        if ($model->load(Yii::$app->request->post())) {

            $model->save();
            return $this->redirect('confirmation');
        } else {
            return $this->render('writeus', [
                'model' => $model,
            ]);
        }
    }

型号:

<?php

    namespace backend\models;

    use Yii;

    class ContactUs extends \yii\db\ActiveRecord
    {
        /**
         * @inheritdoc
         */
        public static function tableName()
        {
            return 'contact_us';
        }

        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [['phone'], 'integer'],
                [['message'], 'string'],
                [['verifyCode'], 'required'],
                [['first_name', 'last_name', 'email', 'verifyCode'], 'string', 'max' => 50],
                [['city', 'country'], 'string', 'max' => 20],
            ];
        }


    }

我从我的应用程序的前端提交表单,它不提交模型而是生成。

http://localhost/New/advanced/frontend/web/index.php?_csrf=QUxBOXotOHQXGBdbLWhOAxQ%2FCg4veVkiMCYrYFdJeUQLABhPLmN5NQ%3D%3D&ContactUs%5Bfirst_name%5D=sam&ContactUs%5Blast_name%5D=ravi&ContactUs%5Bcity%5D=Mumbai&ContactUs%5Bcountry%5D=India&ContactUs%5Bemail%5D=&ContactUs%5Bphone%5D=&ContactUs%5Bmessage%5D=&ContactUs%5BverifyCode%5D= 

我需要像普通模式保存一样提交此表单,但无法提交此表单。导致这个问题的原因是什么?

视图:

<?php $form = ActiveForm::begin(); ?>

    <?= $form->field($model, 'first_name')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'last_name')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'city')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'country')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'email')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'phone')->textInput() ?>

    <?= $form->field($model, 'message')->textarea(['rows' => 6]) ?>

    <?= $form->field($model, 'verifyCode')->textInput(['maxlength' => true]) ?>

    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

    <?php ActiveForm::end(); ?>

查看:

            <!-- Write Us -->
            <div class="col-md-6 bordered_block image_bck grey_border" data-color="#fff">
                <div class="over" data-opacity="0.02" data-color="#121d2a"></div>
                <div class="col-md-12 simple_block text-left">
                    <h3>Write Us</h3>
                    <form id="write_us" class="form">
                        <form id="w0" action="/NewHotel/advanced/frontend/web/index.php?r=site%2Fwriteus" method="post">
<input type="hidden" name="_csrf" value="N19ISUwxWnRnZxFkO15jDlNmA3F/fm9ZQBUMOyV4Oz9HFA4OeFcYMQ==">
    <div class="form-group field-contactus-first_name">
<label class="control-label" for="contactus-first_name">First Name</label>
<input type="text" id="contactus-first_name" class="form-control" name="ContactUs[first_name]" maxlength="50">

<div class="help-block"></div>
</div>
    <div class="form-group field-contactus-last_name">
<label class="control-label" for="contactus-last_name">Last Name</label>
<input type="text" id="contactus-last_name" class="form-control" name="ContactUs[last_name]" maxlength="50">

<div class="help-block"></div>
</div>
    <div class="form-group field-contactus-city">
<label class="control-label" for="contactus-city">City</label>
<input type="text" id="contactus-city" class="form-control" name="ContactUs[city]" maxlength="20">

<div class="help-block"></div>
</div>
    <div class="form-group field-contactus-country">
<label class="control-label" for="contactus-country">Country</label>
<input type="text" id="contactus-country" class="form-control" name="ContactUs[country]" maxlength="20">

<div class="help-block"></div>
</div>
    <div class="form-group field-contactus-email">
<label class="control-label" for="contactus-email">Email</label>
<input type="text" id="contactus-email" class="form-control" name="ContactUs[email]" maxlength="50">

<div class="help-block"></div>
</div>
    <div class="form-group field-contactus-phone">
<label class="control-label" for="contactus-phone">Phone</label>
<input type="text" id="contactus-phone" class="form-control" name="ContactUs[phone]">

<div class="help-block"></div>
</div>
    <div class="form-group field-contactus-message">
<label class="control-label" for="contactus-message">Message</label>
<textarea id="contactus-message" class="form-control" name="ContactUs[message]" rows="6"></textarea>

<div class="help-block"></div>
</div>
    <div class="form-group field-contactus-verifycode required">
<label class="control-label" for="contactus-verifycode">Verify Code</label>
<input type="text" id="contactus-verifycode" class="form-control" name="ContactUs[verifyCode]" maxlength="50">

<div class="help-block"></div>
</div>
    <div class="form-group">
        <button type="submit" class="btn btn-success">Create</button>    </div>

    </form>                    </div>
                </div>
                <!-- Write Us End -->
            </div>
            <!-- Row End -->
        </div>
    </section>
    <!-- Contacts End -->

1 个答案:

答案 0 :(得分:1)

请删除

<form id="write_us" class="form">

从view.Yii2框架将自动生成表单元素html。