2017-07-27 4 views
1

Ich versuche Daten zu aktualisieren, aber wenn ich auf die Senden-Schaltfläche klicke, bekomme ich nur Bad request (#400) missing required parameter: id. Es arbeitete an dem vorherigen Projekt und alle Dateien werden von Gii CRUD erstellt. Ich habe das gleiche Problem, wenn ich versuche, create Daten, aber ich denke, ich mache den gleichen Fehler damit. Was ist falsch? Was ich falsch mache, danke im Voraus! Ausblick:yii2 CRUD ActiveForm sendet keine ID an die Update-Aktion

<?php $form = ActiveForm::begin([ 
         'id' => 'update-form' 
       ]); ?> 
       <?php $languages = Lang::find()->all(); ?> 
    <ul class="nav nav-tabs"> 
     <?php 
     foreach ($languages as $key => $language) { 
      if ($language->default == 1) { 
       echo '<li class="active"><a data-toggle="tab" href="#' . $key . '">' . $language->name . '</a></li>'; 
      } else { 
       echo '<li><a data-toggle="tab" href="#' . $key . '">' . $language->name . '</a></li>'; 
      } 
     } 
     ?> 
    </ul> 
    <div class="tab-content"> 
     <?php 
     foreach ($languages as $key => $language) { 
      if ($language->default != 1) { 
       echo '<div id="' . $key . '" class="tab-pane fade">'; 
       echo '<div class="row">'; 
       echo '<div class="col-sm-6"><div class="form-group">'; 
       echo $form->field($model, 'title_' . $language->url)->widget(RemainingCharacters::classname(), [ 
          'type' => RemainingCharacters::INPUT_TEXT, 
          'text' => Yii::t('app', '{n} characters remaining'), 
          'label' => [ 
           'tag' => 'p', 
           'id' => 'my-counter', 
           'class' => 'myCounter', 
           'invalidClass' => 'error' 
          ], 
          'options' => [ 
           'class' => 'form-control', 
           'maxlength' => true, 
          ] 
         ]); 
       echo '</div></div>'; 
       echo '<div class="col-sm-6"><div class="form-group">'; 
       echo $form->field($model, 'url_' . $language->url)->widget(RemainingCharacters::classname(), [ 
          'type' => RemainingCharacters::INPUT_TEXT, 
          'text' => Yii::t('app', '{n} characters remaining'), 
          'label' => [ 
           'tag' => 'p', 
           'id' => 'my-counter', 
           'class' => 'myCounter', 
           'invalidClass' => 'error' 
          ], 
          'options' => [ 
           'class' => 'form-control', 
           'maxlength' => true, 
          ] 
         ]); 
       echo '</div></div>'; 
       echo '</div>'; 
       echo '<div class="row">'; 
       echo '<div class="col-sm-6"><div class="form-group">'; 
       echo $form->field($model, 'meta_title_' . $language->url)->widget(RemainingCharacters::classname(), [ 
          'type' => RemainingCharacters::INPUT_TEXT, 
          'text' => Yii::t('app', '{n} characters remaining'), 
          'label' => [ 
           'tag' => 'p', 
           'id' => 'my-counter', 
           'class' => 'myCounter', 
           'invalidClass' => 'error' 
          ], 
          'options' => [ 
           'class' => 'form-control', 
           'maxlength' => true, 
          ] 
         ]); 
       echo '</div></div>'; 
       echo '<div class="col-sm-6"><div class="form-group">'; 
       echo $form->field($model, 'meta_description_' . $language->url)->widget(RemainingCharacters::classname(), [ 
          'type' => RemainingCharacters::INPUT_TEXT, 
          'text' => Yii::t('app', '{n} characters remaining'), 
          'label' => [ 
           'tag' => 'p', 
           'id' => 'my-counter', 
           'class' => 'myCounter', 
           'invalidClass' => 'error' 
          ], 
          'options' => [ 
           'class' => 'form-control', 
           'maxlength' => true, 
          ] 
         ]); 
       echo '</div></div>'; 
       echo '<div col-xs-12><div class="form-group">'; 
       $textContent = 'content_' . $language->url; 
       if (!$model->isNewRecord) { 
        $model->$textContent = OutData::showTXT($model->$textContent); 
       } 
       echo $form->field($model, 'content_' . $language->url)->textarea(); 
       echo "<script> 
         CKEDITOR.replace('Page[content_$language->url]'); 
        </script>"; 
       echo '</div></div>'; 
       echo '</div>'; 
      } else { 
       echo '<div id="' . $key . '" class="tab-pane fade in active">'; 
       echo '<div class="row">'; 
       echo '<div class="col-sm-6"><div class="form-group">'; 
       echo $form->field($model, 'title')->widget(RemainingCharacters::classname(), [ 
          'type' => RemainingCharacters::INPUT_TEXT, 
          'text' => Yii::t('app', '{n} characters remaining'), 
          'label' => [ 
           'tag' => 'p', 
           'id' => 'my-counter', 
           'class' => 'myCounter', 
           'invalidClass' => 'error' 
          ], 
          'options' => [ 
           'class' => 'form-control', 
           'maxlength' => true, 
          ] 
         ]); 
       echo '</div></div>'; 
       echo '<div class="col-sm-6"><div class="form-group">'; 
       echo $form->field($model, 'url')->widget(RemainingCharacters::classname(), [ 
          'type' => RemainingCharacters::INPUT_TEXT, 
          'text' => Yii::t('app', '{n} characters remaining'), 
          'label' => [ 
           'tag' => 'p', 
           'id' => 'my-counter', 
           'class' => 'myCounter', 
           'invalidClass' => 'error' 
          ], 
          'options' => [ 
           'class' => 'form-control', 
           'maxlength' => true, 
          ] 
         ]); 
       echo '</div></div>'; 
       echo '</div>'; 
       echo '<div class="row">'; 
       echo '<div class="col-sm-6"><div class="form-group">'; 
       echo $form->field($model, 'meta_title')->widget(RemainingCharacters::classname(), [ 
          'type' => RemainingCharacters::INPUT_TEXT, 
          'text' => Yii::t('app', '{n} characters remaining'), 
          'label' => [ 
           'tag' => 'p', 
           'id' => 'my-counter', 
           'class' => 'myCounter', 
           'invalidClass' => 'error' 
          ], 
          'options' => [ 
           'class' => 'form-control', 
           'maxlength' => true, 
          ] 
         ]); 
       echo '</div></div>'; 
       echo '<div class="col-sm-6"><div class="form-group">'; 
       echo $form->field($model, 'meta_description')->widget(RemainingCharacters::classname(), [ 
          'type' => RemainingCharacters::INPUT_TEXT, 
          'text' => Yii::t('app', '{n} characters remaining'), 
          'label' => [ 
           'tag' => 'p', 
           'id' => 'my-counter', 
           'class' => 'myCounter', 
           'invalidClass' => 'error' 
          ], 
          'options' => [ 
           'class' => 'form-control', 
           'maxlength' => true, 
          ] 
         ]); 
       echo '</div></div>'; 
       echo '<div class="col-xs-12"><div class="form-group">'; 
       if (!$model->isNewRecord) { 
        $model->content = OutData::showTXT($model->content); 
       } 
       echo $form->field($model, 'content')->textarea(); 
       echo "<script> 
         CKEDITOR.replace('Page[content]'); 
        </script>"; 
       echo '</div></div>'; 
       echo '</div>'; 
      } 
      echo '</div>'; 
     } 
     ?> 
    </div> 

    <div class="row"> 
     <div class="col-sm-6"> 
      <?php 
      $mas[0] = Yii::t('app', 'Главна страница'); 
      $mas['----------------'] = ArrayHelper::map(Page::find()->where('id_in=0 and is_cat=1')->all(), 'id', 'title'); 
      ?> 
      <?= 
      $form->field($model, 'id_in')->widget(Select2::classname(), [ 
       'model' => $model, 
       'attribute' => 'id_in', 
       'data' => $mas, 
       'options' => [ 
        'placeholder' => Yii::t('app', 'Select a type ...'), 
       ], 
       'pluginOptions' => [ 
        'allowClear' => true 
       ], 
      ]) 
      ?> 
     </div> 
     <div class="col-sm-6"> 
      <?php 
      if ($model->isNewRecord) { 
       echo $form->field($model, 'view_id')->hiddenInput(['value' => 12])->label(false); 
      } else { 
       echo $form->field($model, 'view_id')->hiddenInput(['value' => $model->view_id])->label(false); 
      } 
      ?> 
     </div> 
    </div> 

    <div class="row"> 
     <div class="col-sm-4"> 
      <?= $form->field($model, 'sort')->textInput() ?> 
     </div> 
    </div> 

    <div class="row"> 
     <div class="col-sm-3"> 
      <?= $form->field($model, 'active')->hiddenInput(['value' => ($model->active) ? $model->active : 2])->label(false) ?> 

      <?= Html::activeLabel($model, 'active', ['class' => 'dblock flLeft mr5 pt5']); ?> 
      <label class="switch switch-custom block dblock flLeft"> 
       <input type="checkbox" value="1" id="active_<?= $model->id ?>"class="legend-switch" <?= ($model->active == 1) ? 'checked="checked"' : '' ?> onchange="changeHiddenField('page-active', 'active_<?= $model->id ?>');"></input> 
       <label data-off="<?= Yii::t('app', 'Не'); ?>" data-on="<?= Yii::t('app', 'Да') ?>" for="active_<?= $model->id ?>"></label> 
      </label> 
     </div> 
     <div class="col-sm-3"> 
      <?= $form->field($model, 'in_main_menu')->hiddenInput(['value' => ($model->in_main_menu) ? $model->in_main_menu : 2])->label(false) ?> 

      <?= Html::activeLabel($model, 'in_main_menu', ['class' => 'dblock flLeft mr5 pt5']); ?> 
      <label class="switch switch-custom block dblock flLeft"> 
       <input type="checkbox" value="1" id="in_main_menu_<?= $model->id ?>"class="legend-switch" <?= ($model->in_main_menu == 1) ? 'checked="checked"' : '' ?> onchange="changeHiddenField('page-in_main_menu', 'in_main_menu_<?= $model->id ?>');"></input> 
       <label data-off="<?= Yii::t('app', 'Не'); ?>" data-on="<?= Yii::t('app', 'Да') ?>" for="in_main_menu_<?= $model->id ?>"></label> 
      </label> 
     </div> 
     <div class="col-sm-3"> 
      <?= $form->field($model, 'in_side_menu')->hiddenInput(['value' => ($model->in_side_menu) ? $model->in_side_menu : 2])->label(false) ?> 

      <?= Html::activeLabel($model, 'in_side_menu', ['class' => 'dblock flLeft mr5 pt5']); ?> 
      <label class="switch switch-custom block dblock flLeft"> 
       <input type="checkbox" value="1" id="in_side_menu_<?= $model->id ?>"class="legend-switch" <?= ($model->in_side_menu == 1) ? 'checked="checked"' : '' ?> onchange="changeHiddenField('page-in_side_menu', 'in_side_menu_<?= $model->id ?>');"></input> 
       <label data-off="<?= Yii::t('app', 'Не'); ?>" data-on="<?= Yii::t('app', 'Да') ?>" for="in_side_menu_<?= $model->id ?>"></label> 
      </label> 
     </div> 
     <?= $form->field($model, 'is_cat')->hiddenInput(['value' => ($model->is_cat) ? $model->is_cat : 0])->label(false) ?> 

    </div> 

</div> 

<div class="form-group"> 
    <?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'app.Create') : Yii::t('app', 'app.Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> 
</div> 

<?php ActiveForm::end(); ?> 

Controller:

public function actionUpdate($id) { 
     $model = $this->findModel($id, true); 

     if ($model->load(Yii::$app->request->post()) && $model->save()) { 
      $model = $this->findModel($model->id, true); 
      $languages = Lang::find()->all(); 
      foreach ($languages as $language) { 
       if ($language->default != 1) { 
        $title = 'title_' . $language->url; 
        // $url = MakeURL::parseUrl($model->$title, $model->id); 
        $urlName = 'url_' . $language->url; 
        if($model->old_link == 1){ 
         $model->$urlName = MakeURL::parseUrl($model->$urlName, $model->id); 
        }else{ 
         $model->$urlName = MakeURL::parseUrlGoogle($model->$urlName, $model->id); 
        } 
        $content = 'content_' . $language->url; 
        $var = Yii::$app->OutData->sanitize($model->$content); 
        $model->$content = $var; 
       } else { 
        // $url1 = MakeURL::parseUrl($model->title, $model->id); 
        if($model->old_link == 1){ 
         $model->url = MakeURL::parseUrl($model->url, $model->id); 
        }else{ 
         $model->url = MakeURL::parseUrlGoogle($model->url, $model->id); 
        } 
        $model->content = Yii::$app->OutData->sanitize($model->content); 
       } 
      } 
      $model->dt_update = date('Y-m-d H:i:s'); 
      $model->update_user_id = Yii::$app->user->identity->id; 
      $model->save(); 
      //return $this->redirect(['index']); 
      return $this->redirect([Yii::$app->session['ReturnUrl']]); 
     } else { 
      return $this->render('update', [ 
         'model' => $model, 
      ]); 
     } 
    } 

Antwort

1
public function actionUpdate($id) { 

Aktion erwartet ein erforderlicher Parameter $id, wenn es heißt, aber sie vorbei sind es nicht (es geht so etwas wie index.php?r=site/update aber es sollte wie sein dies: index.php?r=site/update&id=10). Lösung für dieses Problem ist die folgende Zeile in ActiveForm Parameter hinzuzufügen:

'action' => Url::to(['site/update', 'id' => $id]), 

Wo $id ist die ID des Modells. Und das macht:

<?php 
$form = ActiveForm::begin([ 
    'id' => 'update-form', 
    'action' => Url::to(['site/update', 'id' => $id]), 
]); ?> 

Leider im Voraus, wenn diese irgendwo anders definiert ist, ich habe mich nicht an einer ganzen Code.

+0

Vielen Dank! –

Verwandte Themen