简体   繁体   English

yii 中的基本隐藏字段

[英]Basic Hidden field in yii

I'm trying to place data in hidden text in yii, but I don't know how.我试图在 yii 的隐藏文本中放置数据,但我不知道如何。 I need a similar code to a regular php syntax:我需要一个与常规 php 语法类似的代码:

<input type="hidden" name="field_name" value="a"/>

It's supposed to be a field with static value of a.它应该是一个静态值为 a 的字段。 I just need it to go with my $_POST variables for error checking.我只需要它与我的 $_POST 变量一起进行错误检查。

Is it possible to avoid modifying the models and controllers just to put the field in?I cant use gii cause I only have snippets of code with me.Sorry as well as I have little understanding of yii so I have no clue if what I'm saying about the last 2 sentences is correct.是否可以避免修改模型和控制器只是为了将字段放入?我不能使用 gii 因为我只有代码片段。对不起,我对 yii 知之甚少,所以我不知道我是什么我说最后两句话是正确的。

in views在视图中

hidden field with model and form:带有模型和形式的隐藏字段:

<?php echo $form->hiddenField($model, 'name'); ?>

or without model没有模型

<?php echo CHtml::hiddenField('name' , 'value', array('id' => 'hiddenInput')); ?>

Yii 隐藏输入:

<?php echo $form->hiddenField($model,'fieldName',array('value'=>'foo bar')); ?>

如果来自数据库和值或大小字段的数据:

echo $form->hiddenField($experience,'job_title',array('size'=>'50','value'=>$experience_data['job_title'])); ?>

Yii 1

<?php echo $form->hiddenField($model, 'name'); ?>

Yii2 Yii2

<?= Html::activeHiddenInput($model, 'attribute', ['value' => 'Some Value']) ?>

Also, worth noting for Yii2, the array parameter works different to a normal form field.另外,值得注意的是 Yii2,数组参数的工作方式与普通表单字段不同。 EG A normal input would look more like this. EG 正常输入看起来更像这样。

<?= $form->field($model, 'attribute', ['inputOptions' => ['placeholder' => 'Some Placeholder', 'value' => 'Some Input Value']]) ?>

Hope this helps.希望这可以帮助。

for yii2 you can try this对于 yii2 你可以试试这个

<?= $form->field($model, 'user_type',['inputOptions' => ['value' => '2']])->hiddenInput()->label(false) ?>

It worked for me它对我有用

Alternatively,或者,

echo CHtml::activeHiddenField($model,"[$i]id", array("value" => $model->id));

This would set hidden field value as the id from model.这会将隐藏字段值设置为模型的 id。 The [$i] is useful for multiple record update. [$i] 对于多记录更新很有用。

Here are two ways to do that...这里有两种方法可以做到这一点......

without model无模型

echo CHtml::hiddenField('name' , 'value', array('id' => 'name'));

with model带模型

echo $form->hiddenField($model, 'name');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM