2017-07-27 7 views
1

データを更新しようとしていますが、投稿ボタンをクリックすると、Bad request (#400) missing required parameter: idが表示されます。それは以前のプロジェクトで働いていたし、すべてのファイルはGii CRUDによって作成されています。私はcreateデータにしようとしているときに同じ問題がありますが、私はそれと同じ間違いを犯していると思います。なにが問題ですか?私は間違っていますか?事前にありがとう! ビュー:yii2 CRUD ActiveFormはIDを更新アクションに送信しません

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

コントローラー:

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

答えて

1
public function actionUpdate($id) { 

このアクションは、それを呼び出して必要なパラメータ$idを見込んでいますが、(それがindex.php?r=site/updateのようなものだが、それはようにする必要があり、それを渡していませんこれはindex.php?r=site/update&id=10です)。 $idは、モデルのIDで

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

:その問題を解決するには、ActiveFormパラメータに次の行を追加することです。そして、これは作る:これはどこか別の場所に定義されている場合、事前に

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

は申し訳ありませんが、私は、全体のコードを見ていません。

+0

ありがとうございました! –

関連する問題