activeTextField and activeTextArea - default values

Here is how I set up activeTextField and activeTextArea, with the aim of having default values upon page load:




<?php echo CHtml::activeTextField($contact, 'name', array('value'=>'Your Name')); ?>

<?php echo CHtml::activeTextArea($contact, 'enquiry', array('value'=>'Details of Enquiry')); ?>



Couple of things:

  1. When there is a validation error, the default value is populated in the field, instead of the posted value

  2. I can’t get the default value for the textarea to display

Can anyone advise how to do this?

  1. By setting the value directly you override the value on the $contact model

  2. Text areas do not have a "value" attribute,

Personally I would set the "default" values on the $contact model class itself, and not supply the html attributes.

nz

Cheers, thanks for your help.

How can I check that the posted values are not the default values of the form fields?

In the controler:

in the same way that you put the value?

In the Model:

perhaps creating a variable in the controller and then using it in the model validator

in the controler




public default_value_for_xxxxx='yyyyy';



in the model




    public function rules()

    {

        return array(

                        array('xxxxx','xxxxxValidator'),

        );

    }


public function xxxxxValidator()


    {

           if ($this->default_value_for_xxxxx==$this->xxxxx)

                {

                      .......

                }

    }




I hope is what you want

activeTextArea should be able to support pre-filled values, since it’s based on CHtml::tag, which has a $content parameter. You might want to file an issue ticket for that!

OK in my model I created an attribute:

public $name=‘Your Name’;

Is there a "rule" I can put in the model that will check to see if the submitted value matches $name?

Perhaps declare self::YOUR_NAME and use it in a CCompareValidator (compareValue)?

/Tommy

yes tri, Starting from version 1.0.8, CCompareValidator supports different comparison operators. Previously, it only compares to see if two values are equal or not.

http://www.yiiframework.com/doc/api/1.0.11/CCompareValidator#operator-detail

[EDIT]

see http://www.yiiframework.com/doc/cookbook/56/




public function rules()

    {

        return array(

                        array('xxxxx','compare','compareValue'=>$this->name,'operator'=>'!='),

        );

    }



note: $this->name if you put the default value in the controller

  self::name if you put  the default value in the model

no tested :)

[EDIT2]




public $name='Your Name';



I think the name "name" is not a good idea, is the same as the model

maybe "name_default_value"

any news about the default activeTextarea value ?

is it available on 1.1rc ?

Are you talking about that? see this link

@fouss

Note that you responded to a question posted on 11 january 2010 from a user that has not been active since 14. jun 2010.

Thx for let me know that!