Form with different roles

Hi,

if I have three or more different roles which change data, where is the best point to split an update form.

Example:

field 1 <- role 1

field 2 <- role 1, 2

field 3 <- role 1, 2, 3

field 4 <- role 1, 3

field 5 <- role 1, 3

field 6 <- role 4

Role 1 can change every field, Role 2 the field 2 and 3, and so on. If a role can not change data, the field should be readonly.

I have found serveral solutions.

  1. I create one seperate form for each role.

  2. I work with if-statements in the form.

  3. I create seperate actions/views for each role and split the original form into 3 or more pieces and call them with renderpartial.

  4. Other ideas?

Greetings

Carsten

Hi Carsten,

i'm having exactly the same difficulty to solve as you!

And i also had the same ideas about solving this issue :)

Cause there are many fields on my form and i can split the fields in groups of nearly identical size corresponding to the roles i used a simple solution by just splitting the fields into groups and placing them on different CTabView-Tabs.

These Tabs are then generated/shown on a role basis. This would comply with your second solution.

But this approach is not very flexible nor satisfying, so i hope we can find a better solution for this.  ;)

Greets yoshi

Perhaps extend the CHtml::active…() methods. These functions have access to the model and therefore could ask the model whether the current user has access to this field (and if so what kind of access). Depending on that, you display either the value or the form element.

This problem is interesting and realistic. Let's keep discussing and hope to find a perfect solution.

Ok, here is my suggestion.

I extended CHTML (is E the right character?):

<?php


class EHTML extends CHTML


{


	public static function activeTextField($model,$attribute,$htmlOptions=array(),$filters=array())


	{


		if($filters['active']===true && $filters['fields'][$attribute]!==true) {


                    return self::encode($model->$attribute);                        


                } else {


                    self::resolveNameID($model,$attribute,$htmlOptions);


                    self::clientChange('change',$htmlOptions);


                    return self::activeInputField('text',$model,$attribute,$htmlOptions);


                }


	}


}

Now I can add a filter to field:

<div class="simple">


<?php echo CHtml::activeLabelEx($freightplan,'ordernr'); ?>


<?php echo EHtml::activeTextField($freightplan,'ordernr',array('size'=>50,'maxlength'=>50),$filters); ?>


</div>

In my controller I make a new update-action and create an filter-array that defines which fields are active. I keep the original update-action for use as administrator:

<?php


	public function actionUpdateRole()


	{


            $filters=array('active'=>true,'fields'=>array('ordernr'=>true));


            $this->actionUpdate($filters);


	}





	public function actionUpdate($filters=array())


	{


            ...


            $this->render('update',array(... ,'filters'=>$filters));


            ...


	}





Now i just call …index.php?r=controller/update/role&id=… and every EHtml-field expect 'ordernr' is inactive .

Edit: changed default from active to inactive. Now you have to set the fields which should be editable.