How to solve Bad Request (#400) Unable to verify your data submission in yii2?

给你一囗甜甜゛ 提交于 2019-12-11 03:03:24

问题


I have got this error, when submit the form:

Bad Request (#400) Unable to verify your data submission.

I have got <?= Html::csrfMetaTags() ?> in the layout. I think, that this problems I have, because I use datepicker. Form create with ActiveForm.

What I must to do? Here is code of the form:

<?
        $form2 = ActiveForm::begin(['id' => 'user-univer']);
        echo $form2->field($model2, 'university')->label('Input university name:');
        echo $form2->field($model2, 'degree')->label('Input your education specialization:');
        //echo $form2->field($model2, 'date')->label('Input your education date:');
        echo '<label class="control-label">Education time:</label><br/>';
        echo '<span>Start date of your education:</span>';
        echo DatePicker::widget([
            'name'  => 'date_from',
            'value'  => $value,
            'dateFormat' => 'dd.MM.yyyy',
        ]);
        echo '<span>End date of your education:</span>';
        echo DatePicker::widget([
            'name'  => 'date_to',
            'value'  => $value,
            'dateFormat' => 'dd.MM.yyyy',    
        ]);
        echo '<br/><br/>';


        echo $form2->field($model2, 'info')->textarea()->label('Any other information about your university degree:');
        echo Html::submitButton('Add university', ['class' => 'btn btn-primary btn-univer']);
        ActiveForm::end(['id' => 'user-univer']);

        } ?>

UPD: without datepicker I have got the same problems, why? how to solve it?


回答1:


Add csrf token field in your form

<input type="hidden" name="<?=Yii::$app->request->csrfParam?>" value="<?=Yii::$app->request->getCsrfToken()?>" />

Will solve your problem.




回答2:


problem may be due to csrf validation. Have you tried disabling csrf Validation

inside action try this

$this->enableCsrfValidation = false;

check this link




回答3:


I found that is missing a / in the end of input, ex:

<input type="hidden" name="_csrf" value="bWs1ZlJSeGYiHG0xNThVNgBYBzY8FAI5PS4CVn8lJzMeIkAgKgU0Ig==">

Working input is:

<input type="hidden" name="_csrf" value="bWs1ZlJSeGYiHG0xNThVNgBYBzY8FAI5PS4CVn8lJzMeIkAgKgU0Ig==" /> 

In my case I modified yiisoft/yii2/helpers/BaseHtml:

public static function tag($name, $content = '', $options = [])
{
    if ($name === null || $name === false) {
        return $content;
    }
    if ($name=="input") {$html = "<$name" . static::renderTagAttributes($options) . '/>';}
    else {$html = "<$name" . static::renderTagAttributes($options) . '>';      }

    return isset(static::$voidElements[strtolower($name)]) ? $html : "$html$content</$name>";
}


来源:https://stackoverflow.com/questions/29154544/how-to-solve-bad-request-400-unable-to-verify-your-data-submission-in-yii2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!