2016-07-11 6 views
4

Meine yii2-Anwendung funktionierte bis gestern, aber heute beim Absenden des Formulars wird ein Fehler angezeigt. "Ungültige Anforderung (# 400) Ihre Datenübermittlung konnte nicht verifiziert werden.".Yii2 - Schlechte Anfrage (# 400) Die Datenübergabe konnte nicht verifiziert werden

Ich habe viele solche Fragen auf Stackoverflow gefunden, wo Leute vorschlagen, csrf Validierung zu deaktivieren, ich versuchte deaktivieren csrf Validierung auch. Ich habe sogar meinen Composer aktualisiert, aber es funktioniert nicht.

schlagen Sie bitte eine andere mögliche Lösung vor.

Das ist meine Form Code: -

<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(); 
        ?> 

und das ist mein Aktionscode innerhalb Regler: -

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, 
    ]); 
} 
+0

Haben Sie weitere Informationen über den Fehler von Yii2 oder Apache/Webserver-Logs? – ldg

+0

zeigen respektiert Form und Controller –

+0

Hallo Insane, bitte überprüfen, ich habe meine Form und Action-Code hinzugefügt –

Antwort

6

verwenden:

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

Do not disable CSRF

0

Sie können Verwenden Sie die Konfiguration in Ihrer Hauptkonfigurationsdatei, um die csrf-Validierung in der gesamten Anwendung global zu deaktivieren.

$config = [ 
    'components' => [ 
     'request' => [ 
      'enableCsrfValidation' => false, 
     ], 
    ], 
];