CActiveForm reuse by scenarions

Hello,

I’m thinking to reuse CActiveForm model for creating and editting record. The problem is, that on ‘edit’ scenario I do not need some form fields, which is needed on ‘create’ scenario. So I’m thinking to check scenario in view:




if($model->getScenario() == 'new_record') { 

   echo $form->textField($model, 'settings'); 

}



Is it right to do multiple if’s in view for that case? Or is there another better way?

Thanx for your thoughts.

It should be fine to use conditions to show rows or not, but another approach that is easier to maintain and reusable is to use CForm. CForm renders safe attributes only (for the current scenario). So this would easily solve your issue. Here’s a link that shows you how to build a CForm:

http://www.yiiframework.com/doc/guide/1.1/en/form.builder#basic-concepts

Btw: When using CActiveForm like you do make your life easier by using




if($model->isAttributeSafe('settings'))

     echo $form->textField($model, 'settings');



This way you should be able to reuse your code in both updating and insert scenarios