Undefined Variable :value in _form in yii2

半城伤御伤魂 提交于 2019-12-08 09:01:10

问题


I need to add a textfield which does not belong to the model. So I've added the following code in the form

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

Please tell me how to declare this variable or how can I resolve this. I've tried adding

public $value;

in the model. But the error remains. Please help.

Controller

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>

error -

Undefined variable: value

回答1:


Your Contoller Code should like : -

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
        ]);
}

Then in create.php and update.php Change :-

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


来源:https://stackoverflow.com/questions/37981755/undefined-variable-value-in-form-in-yii2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!