gpt4 book ai didi

php - 如何修复 Yii2 View 中的 "Undefined variable"错误?

转载 作者:行者123 更新时间:2023-12-01 22:26:02 25 4
gpt4 key购买 nike

这是我的 Controller :

class RoomController extends Controller
{
public function actionCreate()
{
$model = new Room();
$modelSave = false;
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
}
return $this->render('create', ['model' => $model,'modelSaved' => $modelSave]);
}
}

这是我的看法

<?php if($modelSave) { ?>
<div class = "alert alert-success" >
Model ready to be saved!
</div>
<?php } ?>
<?php $form = ActiveForm::begin(); ?>
<div class="row">
<div class = "col-lg-12">
<h1>Room Form</h1>
<?= $form->field($model,'floor')->textinput()?>
<?= $form->field($model,'room_number')->textinput() ?>
<?= $form->field($model,'has_conditioner')->checkbox() ?>
<?= $form->field($model,'has_tv')->checkbox() ?>
<?= $form->field($model,'has_phone')->checkbox() ?>
<?= $form->field($model,'available_form')->textinput() ?>
<?= $form->field($model,'price_per_day')->textinput() ?>
<?= $form->field($model,'description')->textarea() ?>
</div>
</div>
<div class="form-group">
<?= Html::submitButton('create',['class'=>'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>

当我检查它是否有效时,ErrorException 显示:

PHP Notice – yii\base\ErrorExceptionUndefined variable: modelSave

我试过调试,但我不知道为什么 $modelSave 不能发送到 View “create”。

最佳答案

你测试

<?php if($modelSave) { ?>

但是在你的渲染中你有

return $this->render('create', ['model' => $model,'modelSaved' => $modelSave]);

使用

<?php if($modelSaved) { ?>

关于php - 如何修复 Yii2 View 中的 "Undefined variable"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34397288/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com