未定义变量:yii2中_form中的值

时间:2016-06-23 03:20:54

标签: yii2

我需要添加一个不属于该模型的文本字段。 所以我在表单

中添加了以下代码
<?= 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

1 个答案:

答案 0 :(得分:3)

您的控制器代码应该如下: -

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
]) ?>