yii2 隐藏输入值
yii2 hidden input value
在 Yii2 中,我正在尝试构造隐藏输入
echo $form->field($model, 'hidden1')->hiddenInput()->label(false);
但我还需要它有一些价值选项,我该怎么做?
您可以使用选项
echo $form->field($model, 'hidden1',
['options' => ['value'=> 'your value'] ])->hiddenInput()->label(false);
更改此处的值没有意义,因为它是活动字段。这意味着值将与模型值同步。
只需更改$model->hidden1
的值即可更改。或者在提交表单后收到用户数据后更改。
使用非活动隐藏输入会像这样:
use yii\helpers\Html;
...
echo Html::hiddenInput('name', $value);
但后者更适合模型外使用
你也可以这样做
$model->hidden1 = 'your value';// better put it on controller
$form->field($model, 'hidden1')->hiddenInput()->label(false);
如果您在控制器上设置值,这是一个更好的选择
$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,
]);
}
使用以下内容:
echo $form->field($model, 'hidden1')->hiddenInput(['value'=> $value])->label(false);
简单的你可以这样写:
<?= $form->field($model, 'hidden1')->hiddenInput(['value'=>'abc value'])->label(false); ?>
我知道它很旧 post 但有时 HTML 没问题 :
<input id="model-field" name="Model[field]" type="hidden" value="<?= $model->field ?>">
请保重
- id :带有 - 而不是 _
的小写字母
- 姓名:大写的第一个字母
像这样:
<?= $form->field($model, 'hidden')->hiddenInput(['class' => 'form-control', 'maxlength' => true,])->label(false) ?>
<?= $form->field($model, 'hidden_Input')->hiddenInput(['id'=>'hidden_Input','class'=>'form-control','value'=>$token_name])->label(false)?>
或
<input type="hidden" name="test" value="1" />
使用这个。
您可以在视图(表单)中使用此代码行
<?= $form->field($model, 'hidden1')->hiddenInput(['value'=>'your_value'])->label(false) ?>
请参考这个例子
如果您需要将 currant 日期和时间作为隐藏输入传递:
模型属性是 'created_on' 并且它的值是从 date('Y-m-d H:i:s') 检索的,
就像:“2020-03-10 09:00:00”
<?= $form->field($model, 'created_on')->hiddenInput(['value'=>date('Y-m-d H:i:s')])->label(false) ?>
你看,使用隐藏输入的主要问题是你想传递什么样的数据?
我假设您正在尝试传递用户 ID。
在此处传递它并不是一个好主意,因为 field() 方法将生成输入
并且该值将显示给用户,因为我们无法从用户浏览器中隐藏 html。如果您真的关心网站的安全性,请这样做。
请检查此 link,您会发现无法隐藏值属性不让用户看到。
那怎么办?
看,这就是PHP中OOP的核心。
我引用了 Matt Zandstr 在他的伟大著作 PHP 对象、模式和实践第五版
中的话
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.
请查看他的书中也提到的使用__construct()方法的这个例子。
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;
}
}
你可以简单地做这个魔术。
$product1 = new ShopProduct("My Antonia","Willa","Cather",5.99 );
print "author: {$product1->getProducer()}\n";
这会产生以下结果:
author: Willa Cather
在您的情况下,这与此类似,每次您创建一个对象时,只需将用户 ID 传递给 user_id 属性,并节省大量编码。
Class Car {
private $user_id;
//.. your properties
public function __construct($title,$firstName,$mainName,$price){
$this->user_id = \Yii::$app->user->id;
//..Your magic
}
}
在 Yii2 中,我正在尝试构造隐藏输入
echo $form->field($model, 'hidden1')->hiddenInput()->label(false);
但我还需要它有一些价值选项,我该怎么做?
您可以使用选项
echo $form->field($model, 'hidden1',
['options' => ['value'=> 'your value'] ])->hiddenInput()->label(false);
更改此处的值没有意义,因为它是活动字段。这意味着值将与模型值同步。
只需更改$model->hidden1
的值即可更改。或者在提交表单后收到用户数据后更改。
使用非活动隐藏输入会像这样:
use yii\helpers\Html;
...
echo Html::hiddenInput('name', $value);
但后者更适合模型外使用
你也可以这样做
$model->hidden1 = 'your value';// better put it on controller
$form->field($model, 'hidden1')->hiddenInput()->label(false);
如果您在控制器上设置值,这是一个更好的选择
$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,
]);
}
使用以下内容:
echo $form->field($model, 'hidden1')->hiddenInput(['value'=> $value])->label(false);
简单的你可以这样写:
<?= $form->field($model, 'hidden1')->hiddenInput(['value'=>'abc value'])->label(false); ?>
我知道它很旧 post 但有时 HTML 没问题 :
<input id="model-field" name="Model[field]" type="hidden" value="<?= $model->field ?>">
请保重
- id :带有 - 而不是 _ 的小写字母
- 姓名:大写的第一个字母
像这样:
<?= $form->field($model, 'hidden')->hiddenInput(['class' => 'form-control', 'maxlength' => true,])->label(false) ?>
<?= $form->field($model, 'hidden_Input')->hiddenInput(['id'=>'hidden_Input','class'=>'form-control','value'=>$token_name])->label(false)?>
或
<input type="hidden" name="test" value="1" />
使用这个。
您可以在视图(表单)中使用此代码行
<?= $form->field($model, 'hidden1')->hiddenInput(['value'=>'your_value'])->label(false) ?>
请参考这个例子
如果您需要将 currant 日期和时间作为隐藏输入传递: 模型属性是 'created_on' 并且它的值是从 date('Y-m-d H:i:s') 检索的, 就像:“2020-03-10 09:00:00”
<?= $form->field($model, 'created_on')->hiddenInput(['value'=>date('Y-m-d H:i:s')])->label(false) ?>
你看,使用隐藏输入的主要问题是你想传递什么样的数据?
我假设您正在尝试传递用户 ID。
在此处传递它并不是一个好主意,因为 field() 方法将生成输入
并且该值将显示给用户,因为我们无法从用户浏览器中隐藏 html。如果您真的关心网站的安全性,请这样做。
请检查此 link,您会发现无法隐藏值属性不让用户看到。
那怎么办?
看,这就是PHP中OOP的核心。
我引用了 Matt Zandstr 在他的伟大著作 PHP 对象、模式和实践第五版
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.
请查看他的书中也提到的使用__construct()方法的这个例子。
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;
}
}
你可以简单地做这个魔术。
$product1 = new ShopProduct("My Antonia","Willa","Cather",5.99 );
print "author: {$product1->getProducer()}\n";
这会产生以下结果:
author: Willa Cather
在您的情况下,这与此类似,每次您创建一个对象时,只需将用户 ID 传递给 user_id 属性,并节省大量编码。
Class Car {
private $user_id;
//.. your properties
public function __construct($title,$firstName,$mainName,$price){
$this->user_id = \Yii::$app->user->id;
//..Your magic
}
}