text field does not return new value

Hi all,

I am not sure if my question is isoteric but I can’t seem to get it working.

I have a date of birth field in my registration form. it is represented as:


<div class="simple">

<?php echo CHtml::activeLabelEx($model,'dob'); ?>

<?php echo CHtml::activeTextField($model,'dob',Users::model()->defaultDate())." - format: mm-dd-yyyy"; ?>

</div>

In my model I have declared the following function, which calls the default value:


public function defaultDate()

    {

        if($this->dob=="")

        {

            return array(

                'value'=>'MM-DD-YYYY',

            );

        }elseif($this->dob!="" && $this->dob != 'MM-DD-YYYY')

        {

            return array(

                'value'=>$this->dob,

            );

        }

    }

When I first load the registration form I get a default value of MM-DD-YYYY in the text field. Then I change it to lets say 10-10-1984, which is absolutely valid date according to my rules. When I click on register the application is being sent to the create action. If there are any outstanding errors I am being returend to the form again and surprisingly I don’t see my last date as the value but I see MM-DD-YYYY in the date of birth again.

Can you please help me clear out where am I making a mistake in my code?

Thanks!

This is because you call defaultDate() on the shared class instance Users::model(). You should use $model instead. BTW you don’t need to set the default explicitly in htmlOptions. Just initialise the attribute $model->dob in the controller (add an else branch for initial load).

Edit:

Another way would be to declare the attribute with initializer in the model.




public $dob = "MM-DD-YYYY";



/Tommy (not a team member)

Thanks Tommy. I declared $dob as a public property in my model and it now works as a charm.