我正在尝试更新数据但是当我点击提交按钮时,我得到的是Bad request (#400) missing required parameter: id
。它正在处理上一个项目,所有文件都由Gii CRUD
创建。当我尝试create
数据时我有同样的问题,但我猜我犯了同样的错误。怎么了?我做错了什么?提前谢谢你!
视图:
<?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,
]);
}
}
答案 0 :(得分:1)
public function actionUpdate($id) {
此操作需要一个必需的参数$id
,当它被调用但你没有通过它时(它类似于index.php?r=site/update
但它应该是这样的:{{1 }})。该问题的解决方案是将以下行添加到index.php?r=site/update&id=10
参数:
ActiveForm
其中'action' => Url::to(['site/update', 'id' => $id]),
是模型的ID。这使得:
$id
如果在其他地方定义了此内容,请提前抱歉,我无法查看整个代码。