46

In Yii2 I'm trying to construct hidden input

echo   $form->field($model, 'hidden1')->hiddenInput()->label(false);

But I also need it to have some value option, how can I do that ?

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
David
  • 4,332
  • 13
  • 54
  • 93

10 Answers10

69

Use the following:

echo $form->field($model, 'hidden1')->hiddenInput(['value'=> $value])->label(false);
Shawn Mehan
  • 4,513
  • 9
  • 31
  • 51
TimLeary
  • 791
  • 5
  • 2
43

Changing the value here doesn't make sense, because it's active field. It means value will be synchronized with the model value.

Just change the value of $model->hidden1 to change it. Or it will be changed after receiving data from user after submitting form.

With using non-active hidden input it will be like that:

use yii\helpers\Html;

...

echo Html::hiddenInput('name', $value);

But the latter is more suitable for using outside of model.

arogachev
  • 33,150
  • 7
  • 114
  • 117
21

simple you can write:

<?= $form->field($model, 'hidden1')->hiddenInput(['value'=>'abc value'])->label(false); ?>
Umar Ali
  • 404
  • 4
  • 14
  • This answer worked for me. It uses the ActiveForm method `field()` as per the question and additionally correctly shows how to specify the value in the `hiddenInput()` method. – russellfeeed Feb 09 '16 at 14:43
15

You can do it with the options

echo   $form->field($model, 'hidden1', 
      ['options' => ['value'=> 'your value'] ])->hiddenInput()->label(false);
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
  • It will change just field container HTML attributes. See [here](http://www.yiiframework.com/doc-2.0/yii-widgets-activefield.html#$options-detail). – arogachev May 10 '15 at 17:26
11

you can also do this

$model->hidden1 = 'your value';// better put it on controller
$form->field($model, 'hidden1')->hiddenInput()->label(false);

this is a better option if you set value on controller

$model = new SomeModelName();

if ($model->load(Yii::$app->request->post()) && $model->save()) {
    return $this->redirect(['view', 'id' => $model->group_id]);
 } else {
    $model->hidden1 = 'your value';
    return $this->render('create', [
        'model' => $model,
    ]);
 }
Ary Wibowo
  • 529
  • 4
  • 10
3

Like This:

<?= $form->field($model, 'hidden')->hiddenInput(['class' => 'form-control', 'maxlength' => true,])->label(false) ?>
3

You can use this code line in view(form)

 <?= $form->field($model, 'hidden1')->hiddenInput(['value'=>'your_value'])->label(false) ?>

Please refere this as example

If your need to pass currant date and time as hidden input : Model attribute is 'created_on' and its value is retrieve from date('Y-m-d H:i:s') , just like:"2020-03-10 09:00:00"

  <?= $form->field($model, 'created_on')->hiddenInput(['value'=>date('Y-m-d H:i:s')])->label(false) ?>
Aruna Sanjeewa
  • 134
  • 1
  • 11
0
<?= $form->field($model, 'hidden_Input')->hiddenInput(['id'=>'hidden_Input','class'=>'form-control','value'=>$token_name])->label(false)?>

or

<input type="hidden" name="test" value="1" />

Use This.

slfan
  • 8,950
  • 115
  • 65
  • 78
prathap
  • 1
  • 1
0

You see, the main question while using hidden input is what kind of data you want to pass? I will assume that you are trying to pass the user ID.
Which is not a really good idea to pass it here because field() method will generate input and the value will be shown to user as we can't hide html from the users browser. This if you really care about security of your website.

please check this link, and you will see that it's impossible to hide value attribute from users to see.

so what to do then?

See, this is the core of OOP in PHP. and I quote from Matt Zandstr in his great book PHP Objects, Patterns, and Practice fifth edition

I am still stuck with a great deal of unwanted flexibility, though. I rely on the client coder to change a ShopProduct object’s properties from their default values. This is problematic in two ways. First, it takes five lines to properly initialize a ShopProduct object, and no coder will thank you for that. Second, I have no way of ensuring that any of the properties are set when a ShopProduct object is initialized. What I need is a method that is called automatically when an object is instantiated from a class.


Please check this example of using __construct() method which is mentioned in his book too.

     class ShopProduct { 
       public $title;   
       public $producerMainName;  
       public $producerFirstName;  
       public $price = 0;
    
       public function __construct($title,$firstName,$mainName,$price) {
            $this->title = $title;
            $this->producerFirstName = $firstName;  
            $this->producerMainName = $mainName; 
            $this->price = $price;
    }
 }
     

And you can simply do this magic.

$product1 = new ShopProduct("My Antonia","Willa","Cather",5.99 );
print "author: {$product1->getProducer()}\n";

This produces the following:

author: Willa Cather

In your case it will be something semilar to this, every time you create an object just pass the user ID to the user_id property, and save yourself a lot of coding.

Class Car {
  private $user_id;
//.. your properties

  public function __construct($title,$firstName,$mainName,$price){
     $this->user_id = \Yii::$app->user->id;
    //..Your magic

   }
}
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
WardNsour
  • 313
  • 1
  • 2
  • 16
-1

I know it is old post but sometimes HTML is ok :

<input id="model-field" name="Model[field]" type="hidden" value="<?= $model->field ?>">

Please take care

  • id : lower caps with a - and not a _
  • name : 1st letter in caps
Patrice Flao
  • 491
  • 5
  • 18
  • You're risking html injection here. When framework methods exist, use them, learn them, trust them. – Harry B Sep 26 '18 at 13:19
  • @HarryB can you explain the risk of HTML infection. I cannot see how plain HTML is insecure. – crafter Sep 17 '19 at 08:23
  • 1
    @crafter The built in Yii methods will automatically encode strings before using them as the value attribute of an input. The code above will put whatever `$model->field` contains into the value input without encoding it first. – Harry B Sep 17 '19 at 08:27
  • 1
    If `$model->field` contained `"><"` Then you'd see a js alert on the page. It could in theory be any js which had been submitted or stored by a visitor. – Harry B Sep 17 '19 at 08:29