2

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?

3 Answers3

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.

Touqeer Shafi
  • 4,418
  • 2
  • 23
  • 41
0

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

inside action try this

$this->enableCsrfValidation = false;

check this link

Community
  • 1
  • 1
Bloodhound
  • 2,720
  • 8
  • 31
  • 66
0

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>";
}
Laurel
  • 5,522
  • 11
  • 26
  • 49