Yii $form->textfield, how to set a default value?

It works on my end:

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

before you field description add this:

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

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().

But, if you want to change, or define a default value after load data from the database, you can use the method 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


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')); ?>

Tags:

Yii