如何解决错误的请求(#400)无法验证您在yii2中提交的数据?
问题描述:
在提交表单时出现此错误:
I have got this error, when submit the form:
Bad Request (#400) Unable to verify your data submission.
我有<?= Html :: csrfMetaTags()?>
。我认为,我有这个问题,因为我使用了datepicker。使用ActiveForm创建表单。
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:没有datepicker我有同样的问题,为什么?如何解决它?
UPD: without datepicker I have got the same problems, why? how to solve it?
答
在您的表单中添加csrf标记字段
Add csrf token field in your form
<input type="hidden" name="<?=Yii::$app->request->csrfParam?>" value="<?=Yii::$app->request->getCsrfToken()?>" />
将解决您的问题。