php - Yii2 - 错误请求 (#400) 无法验证您的数据提交

标签 php yii yii2

我的 yii2 应用程序直到昨天都运行良好,但今天在提交表单时显示错误。 “错误请求 (#400) 无法验证您提交的数据。”。

我在 stackoverflow 上发现了很多这样的问题,人们建议禁用 csrf 验证,我也尝试过禁用 csrf 验证。我什至更新了我的 Composer ,但它仍然无法正常工作。

请提出任何其他可能的解决方案。

这是我的表单代码:-

<h2>Open an Account</h2>
                  <?php
                    $form = ActiveForm::begin([
                            'id' => 'live-account-form',
                            'enableClientValidation' => true,
                            'fieldConfig' => [
                                'template' => '{input}{error}',
                                'options' => [
                                    'tag' => false,
                                ]
                            ],
                            'options' => [
                                'class' => 'form-horizontal'
                            ]
                        ]);
                  ?>

                  <div class="form-group">
                    <label for="signupform-first_name" class="col-sm-3 control-label">First Name*</label>
                    <div class="col-sm-9 field-signupform-first_name">
                        <?= $form->field($model, 'first_name')->textInput(['placeholder' => "Enter First Name"]) ?>  

                    </div>
                  </div> 

                  <div class="form-group">
                    <label for="singupform-last_name" class="col-sm-3 control-label">Last Name*</label>
                    <div class="col-sm-9 field-signupform-last_name">
                        <?= $form->field($model, 'last_name')->textInput(['placeholder' => 'Enter Last Name']) ?> 
                    </div>
                  </div>   

                  <div class="form-group">
                    <label for="signupform-email" class="col-sm-3 control-label">Email*</label>
                    <div class="col-sm-9 field-signupform-email">
                        <?= $form->field($model, 'email')->textInput(['placeholder' => "Enter Email Address"]) ?>
                    </div>
                  </div>

                  <div class="form-group">
                    <label for="signupform-country" class="col-sm-3 control-label">Country*</label>
                    <div class="col-sm-9 field-signupform-country">
                        <?= $form->field($model, 'country')->dropDownList(
                            ArrayHelper::map(PhCountry::find()->all(), 'intid', 'country_name'),
                            [
                                'prompt' => 'Select Country',
                                'onchange' => '$( "select#signupform-country_code" ).html("showLoading");
                                    $.get( "index.php/site/fetch-country-code?id='.'"+$(this).val(), 
                                    function(data) {
                                        $( "#signupform-country_code" ).val(data);
                                    });'
                            ]
                        ) ?>
                    </div>
                  </div>

                  <div class="form-group">
                      <label class="col-sm-3 control-label">Phone Number*</label>
                      <div class="col-sm-9 phone-number-div">
                        <div>
                        <?= $form->field($model, 'country_code')->textInput(['placeholder' => 'Code', 'class' => 'country-code form-control']) ?>
                        </div>
                        <div class="field-signupform-phone">
                        <?= $form->field($model, 'phone')->textInput(['placeholder' => 'Enter Phone Number', 'class' => 'enter-phone-number form-control']) ?>
                        </div>
                      </div>
                    </div>

                    <button type="submit" class="btn btn-default">Create Account</button>
                  <?php
                    ActiveForm::end();
                  ?>

这是我在 Controller 中的操作代码:-

public function actionIndex()
{
    Yii::$app->controller->enableCsrfValidation = false;
    $model = new SignupForm();
    if ($model->load(Yii::$app->request->post())) {
        //print_r($model);
        if ($user = $model->signup()) {
            if($model->sendRegistrationEmail($user)) {
                Yii::$app->session->setFlash('emailSent', 'An email containing confirmation link is sent to your email Address.');
                if (Yii::$app->getUser()->login($user)) {
                    return $this->goHome();
                }    
            } 
        }
        //exit;
    }

    return $this->render('index', [
        'model' => $model,
    ]);
}

最佳答案

使用这个:

public function beforeAction($action) 
{ 
    $this->enableCsrfValidation = false; 
    return parent::beforeAction($action); 
}

Do not disable CSRF

关于php - Yii2 - 错误请求 (#400) 无法验证您的数据提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38299474/

相关文章:

php - 如何在 PHP 或 MySQL 中将字符串转换为列表

php - 按字符数将 MySQL 字段拆分为最接近的完整单词?

php - 如何将模型数据加载到在 Yii 中使用 Ajax 过滤的 Select2 下拉列表

php - Yii 在浏览器选项卡关闭时更新数据库字段

html - 在引导列中并排放置 2 个表单字段

php - Joomla Infinite Scrolling mysql 分页问题

php - 使用 XML 仅显示具有特定属性的产品

linux - 在linux的Yii框架中打开试驾。

csrf - 在 Yii2 中禁用单个操作的 CSRF 验证

javascript - 传递一个 jquery 变量