Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

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?

like image 963
user1436497 Avatar asked Aug 23 '12 16:08

user1436497


2 Answers

It works on my end:

<?= $form->field($model, 'some_field')->textInput(['readonly' => true, 'value' => $model->isNewRecord ? 'Your Value' : $model->some_field]) ?> 
like image 166
Imtiaz Avatar answered Oct 19 '22 00:10

Imtiaz


before you field description add this:

<?php $model->teamlead='my default value'; ?> 
like image 42
zuups Avatar answered Oct 19 '22 02:10

zuups