Skip to main content

UpdateRoleRequest

Form request for validating role update payloads. Supports dto context (id / role_id) so cross-field checks know which role is being updated.

Namespace

JobMetric\Rolix\Http\Requests\Role\UpdateRoleRequest

Overview

UpdateRoleRequest:

  • Uses partial (sometimes) field rules suitable for PATCH-style updates
  • Exposes static rulesFor($input, $context) for dto()
  • Accepts context via setContext(['id' => $roleId]) (also accepts role_id)
  • Enforces demotion protection, hierarchy cycles, unique super, permissions, and rule drivers

authorize() returns true.

Context

KeyPurpose
idRole being updated (preferred)
role_idAlias for id
$data = dto($payload, UpdateRoleRequest::class, ['id' => $role->id]);

Validation Rules (rulesFor)

FieldRuleDescription
typesometimes|string|max:255Change role type (must exist in registry)
parent_idnullable|integer|exists:{rolix.tables.role},idNew parent
namesometimes|string|max:255Display name
descriptionnullable|stringDescription
allowsometimes|arrayAllow list
allow.*string
denysometimes|arrayDeny list
deny.*string
is_defaultsometimes|booleanDefault flag
is_supersometimes|booleanSuper flag
orderingsometimes|integer|min:0Ordering
rulessometimes|arrayReplace rules when present
rules.*.driverrequired_with:rules|stringEvaluator name
rules.*.payloadnullable|arrayPayload

rules() delegates to self::rulesFor($this->all(), $this->context).

Attributes

Same as store: type, parent_id, name, description, allow, deny, is_default, is_super, ordering, rules via rolix::base.fields.*.

Cross-Field Validation (withValidator)

Resolved type: $data['type'] ?? $role?->type ?? 'system'.

Type existence

Unknown type → error on type (rolix::base.exceptions.role_type_not_found), then return.

Super demotion

If the existing role is super and the payload sets is_super to a falsy value:

Error keyTranslation
is_superrolix::base.exceptions.role_is_super_cannot_demote

Unique super

When promoting to super (is_super truthy and the current role is not already super), no other role of the same type may be super (excluding current id).

Error keyTranslation
is_superrolix::base.validation.role.is_super_already_exists

Parent constraints

ConditionError keyTranslation
parent_id === role idparent_idrolix::base.validation.role.parent_self
Type not hierarchicalparent_idrolix::base.validation.role.parent_not_allowed
Parent type mismatchparent_idrolix::base.validation.role.parent_type_mismatch
Assigning parent would create a cycle (via role_paths)parent_idrolix::base.validation.role.parent_cycle

Cycle detection: wouldCreateCycle() checks whether the proposed parent already has a path row with path_id = role.id and level > 0.

Permissions

Permission validation is skipped when:

  • Payload is_super is truthy, or
  • The role is already super and is_super is omitted from the payload

Otherwise allow / deny entries must be registered for the type’s model (same as store).

Rule drivers

Same as store: each rules.$index.driver must pass RuleEvaluatorRegistry::has().

Usage Examples

Partial update

use JobMetric\Rolix\Facades\Role;

Role::update($roleId, [
'name' => 'Senior Editor',
'ordering' => 5,
]);

Replace rules

Role::update($roleId, [
'rules' => [
[
'driver' => 'weekday',
'payload' => ['days' => ['monday', 'tuesday', 'wednesday', 'thursday', 'friday']],
],
],
]);
// Omitting "rules" leaves existing rules unchanged

Controller

use JobMetric\Rolix\Http\Requests\Role\UpdateRoleRequest;
use JobMetric\Rolix\Facades\Role;

public function update(UpdateRoleRequest $request, int $id)
{
return Role::update($id, $request->validated());
}

Direct rulesFor

$rules = UpdateRoleRequest::rulesFor(
['name' => 'X'],
['id' => 15]
);