【问题标题】:Undefined Variable :value in _form in yii2未定义变量:yii2中_form中的值
【发布时间】:2016-10-25 04:55:58
【问题描述】:

我需要添加一个不属于模型的文本字段。 所以我在表单中添加了以下代码

<?= Html::textInput("totaldays", $value) ?>

请告诉我如何声明此变量或如何解决此问题。 我试过添加

public $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>

_form.php

<div class="col-xs-4 col-sm-4 col-lg-4">
                <?= Html::textInput("totaldays", $value) ?>
                </div>

错误 -

Undefined variable: value

【问题讨论】:

  • 在渲染时将$value 传递给Controller ...就像return $this-&gt;render('view' ,['total' =&gt; 'total_value']);
  • @DoubleH 我已经像你说的那样添加了控制器,但仍然出现错误。请让我知道该怎么做。
  • 你能准确说出你是如何使用`_form.php的吗?我在你上面找不到这个文件的使用或包含的代码。

标签: yii2


【解决方案1】:

您的控制器代码应该是这样的:-

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.phpupdate.php 改变:-

<?= $this->render('_form', [
    'model' => $model,
    'value' => $value
]) ?>

【讨论】:

  • 还是同样的错误。你能说出模型、控制器和 _form 文件中的代码是什么吗?我已经在问题中给出了我的代码。
  • 我已经按照你说的改变了控制器,把'你的控制器代码应该是这样的: - public function actionView($id) { return $this->render('view', [ 'model' => $this->findModel($id), 'totaldays' => $value ]);然后在 view.php 中应该有 2 个变量 //@var $model yii\base\Model //@var $totaldays any' 在 vi​​ew.php 文件中。但不太确定在哪里放置 '$this->render('_form' ,['totaldays' => $totaldays]);'。请告诉我
  • 不添加$this-&gt;render('_form' ,['totaldays' =&gt; $totaldays]);如果有部分页面我已经提供了..不需要添加
  • 我面临同样的错误。我已经在问题中发布了我的代码和错误。
  • 我假设您的表单部分总是出现在创建和更新不在视图中的情况下..您是否在视图操作中访问 _form.php.. 如果是,则写入完整的 view.php 文件
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-17
  • 1970-01-01
相关资源
最近更新 更多