CFormModel that consist of CActiveRecord

Hi all,

I had a problem. What would be the best solution in this case:

I have a User model that extends CActiveRecord. It consist of ID, Username, Password and Status fields. In creating this model [Registration process], I prefer to have a form with 2 password fields. These 2 fields will be check whether they are the same or not. Only providing one field is not wise since user may make mistakes in typing. The solution what I can think of is making a RegistrationForm extends CFormModel that consist of ID, Username, Password1 and Password2. By having this, I can validate the Password1 = Password2. Is it possible to make the RegistrationForm extends CFormModel to have User and Password2 as the fields/ How could I access this on the view?

Thanks for your help.

Kind regards,

Daniel

You don’t need another CFormModel if you already have your AR. Both can be used as model for a form. Simply add another field:


public $repeatPassword;



and add a rule:


array('repeatPassword', 'compare', 'compareAttribute'=>'password', 'on'=>'register')

Also note the scenario ‘register’. You can add that scenario to all fields you want to use in your registration form. Then set that scenario when you create your model so that all other fields are considered “unsafe”.

Thanks for the suggestion. Btw, I am not quite understand how the scenario works, can you explain a little bit more about this?

Thank you,

Daniel

Did you read the model section in the guide? It describes scenarios:

http://www.yiiframework.com/doc/guide/form.model

As a round up:

You can use the same model for different forms. With a scenario you can define, which model attributes can be assigned in which situation (a.k.a. scenario). So e.g. you could use a model User for registration and for login. These are 2 scenarios. On registration a lot of attributes should be assignable (a.k.a. "be safe") whereas on login only username, password and repeatPassword should be considered "safe".

Hi Mike,

Thank you very much for your valuable time and quick response. I am sorry for my laziness on reading the docs ;)

Kind regards,

Daniel