未定义变量:yii2中_形式的值
我需要添加一个不属于模型的文本字段。 因此,我在表单中添加了以下代码未定义变量:yii2中_形式的值,yii2,Yii2,我需要添加一个不属于模型的文本字段。 因此,我在表单中添加了以下代码 <?= Html::textInput("totaldays", $value) ?> 在模型中。但错误依然存在。请帮忙 控制器 public function actionView($id) { return $this->render('view', [ 'model' => $this->findModel($id),
<?= Html::textInput("totaldays", $value) ?>
在模型中。但错误依然存在。请帮忙
控制器
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
'totaldays' => $value,
]);
}
view.php
<?php
use yii\helpers\Html;
use yii\widgets\DetailView;
/* @var $this yii\web\View */
/* @var $model frontend\modules\salary\models\Salary */
//@var $model yii\base\Model
//@var $totaldays any
$this->title = $model->s_id;
$this->params['breadcrumbs'][] = ['label' => 'Salaries', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="salary-view">
<h1><?= Html::encode($this->title) ?></h1>
<p>
<?= Html::a('Update', ['update', 'id' => $model->s_id], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Delete', ['delete', 'id' => $model->s_id], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => 'Are you sure you want to delete this item?',
'method' => 'post',
],
]) ?>
</p>
<?= DetailView::widget([
'model' => $model,
'attributes' => [
's_id',
's_date',
's_period',
's_empid',
's_empname',
's_workingdays',
's_leave',
's_holiday',
's_wageperday',
's_totalwage',
's_ovthour',
's_ovtrateperhour',
's_ovtamount',
's_tiffinworkingday',
's_tiffinrateperday',
's_wdtiffinamount',
's_sundayworked',
's_sundayrate',
's_sundaytiffinamount',
's_nightcount',
's_nightallrate',
's_nightallowance',
's_convday',
's_convrate',
's_conveyanceamount',
's_tiffinovtcount',
's_tiffinovtrate',
's_tiffinovtamount',
's_incentivecount',
's_incentiverate',
's_incentive',
's_totalearning',
's_epf',
's_esi',
's_ptax',
's_takehome',
],
]) ?>
</div>
您的控制器代码应该如下所示:-
public function actionCreate()
{
......your rest of code.........
$value = 'my Value';
return $this->render('create', [
'model' => $model,
'value' => $value
]);
}
public function actionUpdate($id)
{
......your rest of code.........
$value = 'my Value';
return $this->render('create', [
'model' => $model,
'value' => $value
]);
}
然后在create.php
和update.php
更改:-
<?= $this->render('_form', [
'model' => $model,
'value' => $value
]) ?>
在渲染时在控制器中传递此$value
,如返回$this->render('view',['total'=>'total_value'])代码>@DoubleH我已经像你说的那样添加了控制器,但是仍然得到了错误。请告诉我该怎么做。你能确切地告诉我你是如何使用`\u form.php的吗。我找不到您上面的代码使用或包含此文件。仍然是相同的错误。你能告诉我模型、控制器和表单文件中的代码是什么吗。我已经给出了问题中的代码。我已经按照您所说的更改了控制器,将“您的控制器代码应该是:-公共函数actionView($id){return$this->render('view',['model'=>this->findModel($id),'totaldays'=>$value]);}然后在view.php文件中应该有2个varaibles/@var$model yii\base\model/@var$totaldays any”。但不太确定在哪里放置“$this->render”(“u form“,['totaldays'=>$totaldays])”。请告诉menot添加$this->render(“'u form',['totaldays'=>$totaldays])代码>我已经提供了如果有一些部分页面。。我也面临同样的错误。我已经发布了我的代码和问题中的错误。因为我假设您的表单部分总是出现在创建和更新不在视图中的情况下。您是否正在访问视图操作中的\u Form.php
。。如果是,则编写完整的view.php
文件
Undefined variable: value
public function actionCreate()
{
......your rest of code.........
$value = 'my Value';
return $this->render('create', [
'model' => $model,
'value' => $value
]);
}
public function actionUpdate($id)
{
......your rest of code.........
$value = 'my Value';
return $this->render('create', [
'model' => $model,
'value' => $value
]);
}
<?= $this->render('_form', [
'model' => $model,
'value' => $value
]) ?>