Form input: default value overrides $model value

Hi all :)

I have a text/password input where I want to provide a default value as help to the user. No problem so far:

    <?php echo $form->passwordField($model, 'password', array(

        'id' => 'password',

        'value' => 'Password',

    )); ?>

But know this ‘Password’ value overrides the value coming back from the $model when the form has been sent, but is returned because of an error.

Anyone knows of a way to display the default value, but only when the form has not been sent/returned already?



in your model declare password with a default value

MyModel extends CModel{

public $password='defaultValue' ;

//the rest of your code


or use the code you posted changing

'value' => 'Password',


'value' => $model->isNewRecord ? 'Password' : false,//false or null, I'm not sure

Thanks for your answer Gustavo.

Since it has to do primary with the view, I would rather use the second method you suggested. Unfortunately I get the following error:

I think isNewRecord is only available with AR…

I am using the LoginForm/UserIdentity code dynamically generated by Yii on application creation.

uhmm its a CFormModel and not a model

then try the following, should work

'value' => empty($model->password) ? 'Password' : false,//false or null, I'm not sure

I now use

'value' => (isset($model->username)) ? $model->username : 'Username',

and it works. Thanks for the tip in the right direction :)

Edit: Just saw your second post.

'value' => empty($model->password) ? 'Password' : null // not false

works too, and is more beautiful. Thanks!