misterspelik/yii2-role-activeform

Role based ActiveForm allowing field to be editable or readoonly depending on role

v0.1.0 2018-02-07 10:01 UTC

This package is not auto-updated.

Last update: 2024-11-18 07:53:44 UTC


README

This is a package to be used with Yii2 form widget. Usage is totally the same as Yii2 ActiveForm widget.

Installation

composer require misterspelik/yii2-role-activeform

Configuration

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']
        ],
    ];
}

Usage

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();