Basic Hidden field in yii
Asked Answered
S

8

28

I'm trying to place data in hidden text in yii, but I don't know how. I need a similar code to a regular php syntax:

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

It's supposed to be a field with static value of a. I just need it to go with my $_POST variables for error checking.

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.

Storyteller answered 22/9, 2013 at 13:26 Comment(1)
yiiframework.com/doc/guide/1.1/en/… .this may helpReedreedbird
P
58

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')); ?>
Prinz answered 22/9, 2013 at 13:46 Comment(0)
J
10

Yii hidden input :

<?php echo $form->hiddenField($model,'fieldName',array('value'=>'foo bar')); ?>
Jocularity answered 22/9, 2013 at 13:32 Comment(0)
L
10

In Yii2 this has changed too:

<?= Html::activeHiddenInput($model, 'name') ;?>

References:

http://www.yiiframework.com/forum/index.php/topic/49225-activeform-how-do-you-call-label-input-and-errors-individually/

https://github.com/yiisoft/yii2/issues/735

Loaded answered 23/10, 2014 at 14:11 Comment(0)
O
3

if data from database and value or size field:

echo $form->hiddenField($experience,'job_title',array('size'=>'50','value'=>$experience_data['job_title'])); ?>
Oyez answered 8/12, 2013 at 17:0 Comment(0)
J
3

Yii 1

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

Yii2

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

Also, worth noting for Yii2, the array parameter works different to a normal form field. E.G. A normal input would look more like this.

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

Hope this helps.

Jarv answered 25/1, 2015 at 7:6 Comment(0)
A
3

for yii2 you can try this

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

It worked for me

Abalone answered 30/8, 2016 at 11:45 Comment(1)
Works, but adds DIV.form-group and therefore takes place in the form visual...Dimitry
F
1

Alternatively,

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

This would set hidden field value as the id from model. The [$i] is useful for multiple record update.

Frederigo answered 23/6, 2014 at 9:27 Comment(0)
C
0

Here are two ways to do that...

without model

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

with model

echo $form->hiddenField($model, 'name');
Correa answered 14/3, 2016 at 11:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.