Alternate Form Views - Best Practice?


Been totally new to the Yii framework I have followed the tutorials, books and videos and was wondering if anyone could help with the following best practice question?

We have 4 user types

Each user can create a record but should be restricted to the information they can submit as some will be able to add additional information into the same record.

We have used Gii to create our basic CRUD structure and was wondering if we could extend the create.php view by creating a separate _form.php for each user form view?

so we would have the views:





And each view would reference it’s own _form:

<?php $this->renderPartial(’_form_1’, array(‘model’=>$model)); ?>

<?php $this->renderPartial(’_form_2’, array(‘model’=>$model)); ?>

<?php $this->renderPartial(’_form_3’, array(‘model’=>$model)); ?>

<?php $this->renderPartial(’_form_4’, array(‘model’=>$model)); ?>

And the controller would have reference to:

public function actionCreate1()

public function actionCreate2()

public function actionCreate3()

public function actionCreate4()

Any help would be most appreciated


Honestly, your design is a lazy design. You have to define accessRules for all each actions. And when you do the $model->save(), you need make sure all elements can pass the validation check before save.

So the better way is assigning user type during the user log-in, so it can be access by Yii::app()->user->type. Depends on your statement, user type may be used very often in your system. Once you can handle user->type, then, you only need to create one actionCreate, and one create.php file, but in create.php, you can determine the user type (Yii::app()->user->type) to show different renderPartials.

Old code:

<?php $this->renderPartial('_form', array('model'=>$model)); ?>

New code:


if(Yii::app()->user->type=="type1")$this->renderPartial('_form1', array('model'=>$model));

if(Yii::app()->user->type=="type2")$this->renderPartial('_form2', array('model'=>$model));

if(Yii::app()->user->type=="type3")$this->renderPartial('_form3', array('model'=>$model));

if(Yii::app()->user->type=="type4")$this->renderPartial('_form4', array('model'=>$model));


Maybe it's easier.

Thank you for your advice, that makes things much cleaner and easy to understand.