ghadad
(Golanh)
December 8, 2010, 8:56pm
1
Hello all ,
Is there any way to create an element with default/initial value ?
for example ( that not works ) :
‘elements’=>array(
'owner_id'=>array(
'type'=>'hidden',
'value' => '999',
) ,
…
…
Thanks
Golan
sumwai
(Sumwai Low)
December 9, 2010, 6:05am
2
You can set it from your controller, like this
$form->model->owner_id
where $form is a CForm object,
or you can set default values inside your rules function.
ghadad
(Golanh)
December 9, 2010, 7:27am
3
Thank you . Funny . i didn’t think about that …
sumwai:
You can set it from your controller, like this
$form->model->owner_id
where $form is a CForm object,
or you can set default values inside your rules function.
sumwai, et al, I’m trying to get my form builder form to display default values, and I’d like to keep this type of thing out of the controller. However, when I try to set default values in my rules, they don’t appear in my form (!). Your method of encoding at the controller level works fine, though.
Is there some way that form builder is not relating to the model in the usual way? I ask this because I have some beforeValidate() code in my model, which is not getting executed before validation either.
Or is there something I’m missing…?
sumwai, et al, I’m trying to get my form builder form to display default values, and I’d like to keep this type of thing out of the controller. However, when I try to set default values in my rules, they don’t appear in my form (!). Your method of encoding at the controller level works fine, though.
Is there some way that form builder is not relating to the model in the usual way? I ask this because I have some beforeValidate() code in my model, which is not getting executed before validation either.
Or is there something I’m missing…?
Found the answer to my question. The validation rules only activate upon validation — meaning they don’t execute before displaying a form :
http://www.yiiframew …alue#entry95870
(doh…!)
Actually you can still do this on the view as follows:
$requestor=Yii::app()->user->name;
<?php echo $form->hiddenField/TextField($model,‘requestor’,array(‘value’=>$requestor)); ?>