提交表单时出现此错误:
Bad Request (#400) Unable to verify your data submission.
我的布局中有<?= Html::csrfMetaTags() ?>
。我想,我有这个问题,因为我使用了datepicker。使用ActiveForm创建表单。
我必须做什么?这是表格的代码:
<?
$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我遇到了同样的问题,为什么?怎么解决呢?
答案 0 :(得分:1)
在表单中添加csrf标记字段
<input type="hidden" name="<?=Yii::$app->request->csrfParam?>" value="<?=Yii::$app->request->getCsrfToken()?>" />
将解决您的问题。
答案 1 :(得分:0)
答案 2 :(得分:0)
我发现在输入结束时缺少/
,例如:
<input type="hidden" name="_csrf" value="bWs1ZlJSeGYiHG0xNThVNgBYBzY8FAI5PS4CVn8lJzMeIkAgKgU0Ig==">
工作输入是:
<input type="hidden" name="_csrf" value="bWs1ZlJSeGYiHG0xNThVNgBYBzY8FAI5PS4CVn8lJzMeIkAgKgU0Ig==" />
就我而言,我修改了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>";
}