yii2 hidden input value
Asked Answered
E

10

47

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 ?

Elvera answered 10/5, 2015 at 15:55 Comment(0)
Q
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.

Quarterage answered 10/5, 2015 at 17:23 Comment(1)
use this -> Html::activeHiddenInput($model, 'memid', ['value' => Yii::$app->user->identity->id]);Herron
M
71

Use the following:

echo $form->field($model, 'hidden1')->hiddenInput(['value'=> $value])->label(false);
Metrical answered 9/9, 2015 at 16:25 Comment(0)
Q
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.

Quarterage answered 10/5, 2015 at 17:23 Comment(1)
use this -> Html::activeHiddenInput($model, 'memid', ['value' => Yii::$app->user->identity->id]);Herron
M
21

simple you can write:

<?= $form->field($model, 'hidden1')->hiddenInput(['value'=>'abc value'])->label(false); ?>
Moldy answered 5/1, 2016 at 6:57 Comment(1)
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.Paolo
A
16

You can do it with the options

echo   $form->field($model, 'hidden1', 
      ['options' => ['value'=> 'your value'] ])->hiddenInput()->label(false);
Ara answered 10/5, 2015 at 17:15 Comment(1)
It will change just field container HTML attributes. See here.Quarterage
I
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,
    ]);
 }
Improve answered 13/5, 2015 at 9:0 Comment(1)
One of the good way to assign dynamic values to input hidden field.Attribution
H
3

Like This:

<?= $form->field($model, 'hidden')->hiddenInput(['class' => 'form-control', 'maxlength' => true,])->label(false) ?>
Hidebound answered 5/2, 2019 at 5:23 Comment(0)
A
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) ?>
Aucoin answered 10/4, 2020 at 3:33 Comment(0)
G
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.

Goldfinch answered 11/10, 2019 at 13:37 Comment(0)
P
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

   }
}
Persian answered 5/2, 2021 at 22:35 Comment(0)
S
-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
Surprisal answered 12/9, 2018 at 21:48 Comment(4)
You're risking html injection here. When framework methods exist, use them, learn them, trust them.Dickens
@HarryB can you explain the risk of HTML infection. I cannot see how plain HTML is insecure.Bashibazouk
@Bashibazouk 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.Dickens
If $model->field contained "><script>alert('hello!');</script><" 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.Dickens

© 2022 - 2024 — McMap. All rights reserved.