未定义变量:yii2 中_form 中的值 [英] Undefined Variable :value in _form in yii2

查看:18
本文介绍了未定义变量:yii2 中_form 中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

public $value;

在模型中.但错误仍然存​​在.请帮忙.

控制器

公共函数actionView($id){返回 $this->render('view', ['模型' =>$this->findModel($id),'总天数' =>$价值,]);}

view.php

title = $model->s_id;$this->params['breadcrumbs'][] = ['label' =>'薪水', 'url' =>['指数']];$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], ['类' =>'btn btn-危险','数据' =>['确认' =>'你确定要删除这个项目吗?','方法' =>'邮政',],]) ?></p><?= DetailView::widget(['模型' =>$模型,'属性' =>['s_id','s_date','s_期间','s_empid','s_empname','s_workdays','s_leave','s_holiday','s_wageperday','s_totalwage','s_ovthour','s_ovtrateperhour','s_ovtamount','s_tiffinworkingday','s_tiffinrateperday','s_wdtifinamount','s_sundayworked','s_sundayrate','s_sundaytiffinamount','s_nightcount','s_nightallrate','s_nightallowance','s_convday','s_convrate','s_conveyanceamount','s_tiffnovtcount','s_tiffnovtrate','s_tiffinovtamount','s_incentivecount','s_incentiverate','s_incentive','s_totalearning','s_epf','s_esi','s_ptax','s_takehome',],]) ?>

_form.php

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

错误 -

未定义变量:值

解决方案

你的控制器代码应该是:-

公共函数 actionCreate(){......你的其余代码......$value = '我的价值';返回 $this->render('create', ['模型' =>$模型,'价值' =>$值]);}公共函数 actionUpdate($id){......你的其余代码......$value = '我的价值';返回 $this->render('create', ['模型' =>$模型,'价值' =>$值]);}

然后在 create.phpupdate.php改变:-

render('_form', ['模型' =>$模型,'价值' =>$值]) ?>

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

解决方案

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

这篇关于未定义变量:yii2 中_form 中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆