This is a package to be used with Yii2 form widget. Usage is totally the same as Yii2 ActiveForm widget.
composer require misterspelik/yii2-role-activeform
For example you have model User
and you want to allow edit all fields to admin
role but don't want to allow edit name field to role manager
.
For that you need to define roleRules
method with such content
public function roleRules()
{
return [
[
'allow' => true,
'attributes' => ['*'],
'roles' => ['admin']
],
[
'allow' => false,
'attributes' => ['name'],
'roles' => ['manager']
],
];
}
To include widget to your form just use this namespace and create $form instance
use misterspelik\widgets\RoleActiveForm;
$form = RoleActiveForm::begin([
'role' => 'manager' //current user role
]);
echo $form->field($model, 'name')->textInput(['maxlength' => true]);
// some code here
RoleActiveForm::end();