Partial form view strategy


I try to write the cleanest application possible (dry) and I want your advice about the partial views.

Here is the fields I want to render in a form :



repeat password







The problem is that I need to render this form in multiple actions :







And the fields are not used in every actions. For example :

loginAction=>username & password

registerAction=>all fields except role

changepasswordAction=>password & repeat password

profileAction=>firstname & lastname & mail & notification & avatar        

updateAction=>all fields execpt password & repeat password

retrievePassword=>username & mail

I tried to write a _form partial rendered in every actions by filtering the fiels with the following operators : isNewRecord, scenario, checkAccess. But the _form file is now full of operators and a mess.

Do you have any advice about the _partial strategy I need to use ?

Thanks :)

I have the same problem. Of course you will use model scenarios to determine which fields are required when… but so far as reusing the view code without a zillion conditionals…

Could you use renderPartial in the view to bring in the additional parts of the view and use controller logic to determine which parts need to be included?

Thats not very satisfactory either. Maybe someone else has a better idea. Right now I’m just making copies of the view with slight tweaks. It would be nice if there was a way to generate a view for each scenario automatically.

Actually I think that cut the form in multiple views and add the fragments you want in the main view by using renderPartial is almost the same as making copies of the form.

So for now I think I will make copies of the form. It is not "dry" but I think it is still better than an unreadable view file !

If someone has a solution, let us know plz :)

PS : someone knows how I can prevent the password fields to be populated ?

I don’t show password fields on the profile. Updating the password is its own page/action.

I don’t show password fields on the profile. Updating the password is its own page/action.

I agree. Don’t let DRY torture you. :D

I don’t hesitate to do things “NOT DRY”, when doing “DRY” doesn’t make things simpler.

I think it’s better to have a dedicated form for each action in your case.

You can set the password to empty after you have loaded the model.

$model = User::model()->findByPk($id);

$model->password = '';