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
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')); ?>
Please use array('readonly' => true)
instead of disabled
.
before you field description add this:
<?php
$model->teamlead='my default value';
?>
<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
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
It works on my end:
<?= $form->field($model, 'some_field')->textInput(['readonly' => true, 'value' => $model->isNewRecord ? 'Your Value' : $model->some_field]) ?>