$ Yii $form->textfield,如何设置默认值?

时间:2022-03-22 22:37:37

So I am fiddling with the yii framework and in one of the views,specifically the create form, I am trying to give one of my textfields a default value. Therefore when I go onto my create page the values are already preloaded on the form.

所以我在修改yii框架,在其中一个视图中,特别是create表单中,我试图给我的一个textfields一个默认值。因此,当我进入创建页面时,这些值已经被预加载到表单中。

Here is my current code

这是我当前的代码

<div class="row">
    <?php echo $form->labelEx($model,'teamlead'); ?>
    <?php echo $form->textField($model,'teamlead',array('size'=>50,'maxlength'=>50,'value'=>Yii::app()->user->getUsername(),'disabled'=>'disabled')); ?>
    <?php echo $form->error($model,'teamlead'); ?>
</div>

When I press create,YII gives me an error telling me that there TextField is empty? Not sure what else I can do other than set the value. Am I also suppose to set the model attributes??

当我按下create,YII会给我一个错误告诉我TextField是空的?不知道除了设置值我还能做什么。我是否也应该设置模型属性?

7 个解决方案

#1


18  

before you field description add this:

在字段描述之前,添加以下内容:

<?php
$model->teamlead='my default value';
?>

#2


16  

It works on my end:

它对我起作用:

<?= $form->field($model, 'some_field')->textInput(['readonly' => true, 'value' => 'Your Value']) ?>

#3


4  

Here is my code that I am sending fixed value into database and show that value readonly.

这是我的代码,我正在向数据库发送固定值并显示该值只读。

<?php echo $form->textField($model,'pp_status', array('value'=>'Open', 'readonly' => 'true')); ?>

#4


4  

Please use array('readonly' => true) instead of disabled.

请使用数组('readonly' => true)而不是禁用。

#5


3  

Always, is a good idea deal with data (defaul values, change after something happening, data treatment, etc) on the model class.

总是,处理模型类上的数据(反向值、发生事件后的更改、数据处理等)是一个好主意。

If you're getting the value from after initialize the model, the best way is to use the method init().

如果在初始化模型后得到了值,最好的方法是使用init()方法。

But, if you want to change, or define a default value after load data from the database, you can use the method afterFind()

但是,如果您希望更改或定义从数据库加载数据后的默认值,可以使用afterFind()方法

For example:

例如:

public function afterFind(){
    $this->localdate = date("Y-m-d");
    parent::afterFind();
}

This link has a lot of usefull information about these methods: http://www.yiiframework.com/doc/guide/1.1/en/database.ar#customization

这个链接有很多关于这些方法的有用信息:http://www.yiiframework.com/doc/guide/1.1/en/database.ar#定制。

#6


0  

I believe the MVC way to do this is to place your default value either in your Model:

我认为MVC的方法是将默认值放在模型中:

class MyModel extends \yii\db\ActiveRecord
{
    public $teamlead = 'my default value';
    ....
}

Or in your controller:

或者在你的控制器:

class MyModelController extends Controller
{
    public function actionCreate()
    {
        $model = new MyModel ();
        $model->teamlead = 'my default value';
        ...
    }
}

#7


-1  

<div class="row">
    <?php echo $form->labelEx($model,'teamlead'); ?>
    <?php echo $form->textField($model,'teamlead',array('readonly'=>'true',size'=>50,'maxlength'=>50,'value'=>Yii::app()->user->getUsername(),'disabled'=>'disabled')); ?>
    <?php echo $form->error($model,'teamlead'); ?>
</div>

put array('readonly'=>'true') in your coding it will work

在编码中加入数组('readonly'=>'true')就可以了

#1


18  

before you field description add this:

在字段描述之前,添加以下内容:

<?php
$model->teamlead='my default value';
?>

#2


16  

It works on my end:

它对我起作用:

<?= $form->field($model, 'some_field')->textInput(['readonly' => true, 'value' => 'Your Value']) ?>

#3


4  

Here is my code that I am sending fixed value into database and show that value readonly.

这是我的代码,我正在向数据库发送固定值并显示该值只读。

<?php echo $form->textField($model,'pp_status', array('value'=>'Open', 'readonly' => 'true')); ?>

#4


4  

Please use array('readonly' => true) instead of disabled.

请使用数组('readonly' => true)而不是禁用。

#5


3  

Always, is a good idea deal with data (defaul values, change after something happening, data treatment, etc) on the model class.

总是,处理模型类上的数据(反向值、发生事件后的更改、数据处理等)是一个好主意。

If you're getting the value from after initialize the model, the best way is to use the method init().

如果在初始化模型后得到了值,最好的方法是使用init()方法。

But, if you want to change, or define a default value after load data from the database, you can use the method afterFind()

但是,如果您希望更改或定义从数据库加载数据后的默认值,可以使用afterFind()方法

For example:

例如:

public function afterFind(){
    $this->localdate = date("Y-m-d");
    parent::afterFind();
}

This link has a lot of usefull information about these methods: http://www.yiiframework.com/doc/guide/1.1/en/database.ar#customization

这个链接有很多关于这些方法的有用信息:http://www.yiiframework.com/doc/guide/1.1/en/database.ar#定制。

#6


0  

I believe the MVC way to do this is to place your default value either in your Model:

我认为MVC的方法是将默认值放在模型中:

class MyModel extends \yii\db\ActiveRecord
{
    public $teamlead = 'my default value';
    ....
}

Or in your controller:

或者在你的控制器:

class MyModelController extends Controller
{
    public function actionCreate()
    {
        $model = new MyModel ();
        $model->teamlead = 'my default value';
        ...
    }
}

#7


-1  

<div class="row">
    <?php echo $form->labelEx($model,'teamlead'); ?>
    <?php echo $form->textField($model,'teamlead',array('readonly'=>'true',size'=>50,'maxlength'=>50,'value'=>Yii::app()->user->getUsername(),'disabled'=>'disabled')); ?>
    <?php echo $form->error($model,'teamlead'); ?>
</div>

put array('readonly'=>'true') in your coding it will work

在编码中加入数组('readonly'=>'true')就可以了