default value in CActiveForm's textField()

Hello everyone!

Can anyone tell me how to add default value in this code:




<?php echo $form->textField($model,'adresa',array('class'=>'input input_r input_pryk')); ?>



Just add the value you need to $model->adresa…


<?php echo $form->textField($model,'adresa',array('class'=>'input input_r input_pryk', 'value'=>'custom value')); ?>

try the above code

I tried this already. Doesn’t work.

Itried this but I need this value to be displayed in my textField but it’s not displayed. :(

Should be displayed… post your code


<?php echo $form->labelEx($model,'adresa',array('class'=>'mitka')); ?>

<?php   $model->adresa = "0(342)552786";

        echo $form->textField($model,'adresa',array('class'=>'input input_r input_pryk')); ?>

<?php echo $form->error($model,'adresa',array('class'=>'pomylka')); ?>

with your code I’m getting displayed “hello” in the textbox because of the ‘value’=>‘hello’

If you don’t get anything displayed check your CSS, try without additional classes (input input_r input_pryk)

Yes, mdomba, you are right. Whithout CSS it works - either your method ($model->adresa=…) and bettor’s (‘value’=>‘custom value’). But how does my css influence the code?

Here is my css:




.input

{

	border-style: solid;

	width: 140px;

	font-size: 0.875em;

	font-family: tahoma;

	border-color: #848484;

	border-width: 1px;

	color: #424242;

}


.input_r

{

    width: 200px

}


.input_pryk

{

    color: #A0A49D;

}



I tried your CSS, I see the text, but the color #A0A49D is too light… maybe because of that you don’t see the value… try to change it to #000000 (black) just to see if that is the problem…

Anyway the $model->adresa can be set in the controller before rendering the view…

I tried $model->adresa=… in controller too but with my css it’s not displayed.

As to the color I use, it’s light because the displayed value must serve as an example. So when the input is clicked it disappears and a user can input his value. So color is light but still visible.

What is wrong with my CSS or is it some bug in textField()?

By removing your CSS you see the value so it’s something with your CSS… I tryed to add your css posted above and I see the value… if you don’t see it than there is something else in your CSS for these classes…

Have you tryed the color:#000? Do you see the value with color 000?

If not create a new class with just the color you need and try with that class… You have to find what is causing value to not display…

I found the solution :)

I just had to rename my .input css class. it just didn’t like the name ‘input’. :)

Thank you.




<?php echo $form->labelEx($model,'adresa',array('class'=>'mitka')); ?>

<?php   $model->adresa = "0(342)552786";

        echo $form->textField($model,'adresa',array('class'=>'input input_r input_pryk')); ?>

<?php echo $form->error($model,'adresa',array('class'=>'pomylka')); ?>



I just copied a pasted th above code in my form and replaced the ‘adresa’ to fit my model and It’s working find.

thx!

@fouss

Not only you are replying to an old post, but you are just copying and pasting the same code that is already on this topic… what is the use of that ?

one more thing… to respect the MVC guidelines… the view is not a good place to set an attribute to a default value… this was only to demonstrate how it works…

thank you. it works

greatest forum ::)

In your model add the initial value of the property.

I prefer put this in constructor




<?php 

$this->adresa = 'some value';

?>



I have this form field in a CActive form:




<div class="wrap">

	<?= $form->labelEx($model,'naam'); ?>

	<?= $form->textField($model,'naam',array('size'=>40,'maxlength'=>255)); ?>

	<?= $form->error($model,'naam'); ?>

</div>

When I want to edit this property it is not shown in my textfield, but when I use


var_dump

to check if the property is set, I get the expected value. What I did notice is that this only happens when the value contains a character like é. When the property contains just all normal characters the property is shown in the textfield as expected.

Is this a bug? Or does someone knows this problem and how to solve it?

Kind regards,

Pim