Best Way To Change Form

Hello from Czech republic!

I’m just starting learning Yii and I came across following problem:

What is the best way to make small change in form (like "hide" one field)?

Example: I have standard Yii-generated user registration and login, and now I wanna change my user update action, so that it wouldn’t be possible to change username.

I have several ideas:

Example: I have standard Yii-generated user registration and login, and now I wanna change my user update action, so that it wouldn’t be possible to change username.

  1. Write something like



<?php

if (!$model->isNewRecord()) {

?>

...show username field...

<?php } ?>



in my views/user/_form.php

However, this solution doesn’t seem to be very “clean” to me, cause I think it’s not really strict MVC since I’m putting conditions into view.

  1. Create _formUpdate.php and use different form for Update action. This doesn’t seem to be very good either because it means duplicated code (both _form.php and _formUpdate.php would be essentially the same, except for the one field).

  2. Something similar like 2, except only have _formCreate.php with the first field and then include _form.php (which would have all fields except username). (Btw, what is the best way to do that? Can I use standard PhP include, or is there some mechanics for that in Yii?)

So, my question is, whether is there some better way how to achieve this with Yii? And if not, which way out of my three would you choose?

Thanks!

It really depends how clean you want your solution to be. In this instance, I would probably choose the first option. This use case is unlikely to get significantly more complicated, so the slight bit of logic in the view seems fine to me, and may be simpler to maintain than having separate views with a common include file. If you find that you needed to code further changes in future, it might then be worthwhile refactoring.

Ok, thanks for the advise. I’m not really sure how clean I want it to be, guess the cleaner the better, not for too high prices though (like too much work for nothing).

Think I’ll follow your advice and go with the first solution, at least for now.