Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

XSkinner's avatar

L5.1 Validate Array of Inputs | Roles and Permissions

Hello.

I'm implementing an Role/Permission user interface to add certain permission sets to a given role. I have achieved this after some hours of trying here and there, but know I'm facing a problem: How to handle validation for an array of input fields.

Lets explain a bit my code, I have a view where a form is rendered and one of the inputs, a checkbox exactly, is the permissions available to the given role, so if the role has not several permission sets those appears there take a look to the view:

                <h6 class="teal-text darken-3">AVAILABLE PERMISSIONS</h6>
            @if(count($permissions) > 0)
                {!! Form::open(['action' => ['Admin\RolesController@addPermissions']]) !!}
                {!! Form::hidden('role_id', $role->id, ['id' => 'role_id']) !!}
                @foreach($permissions as $perm)
                    <div class="row">
                        <div class="input-field col s12 m4">
                            {!! Form::checkbox('permission_id['.$perm->id.']', $perm->id, null,['id' => 'permission_id['.$perm->id.']']) !!}
                            {!! Form::label('permission_id['.$perm->id.']', $perm->area.'.'.$perm->permission) !!}
                        </div>
                        <div class="input-field col s12 m8">
                            {!! Form::text('actions['.$perm->id.']', $perm->actions, ['id' => 'actions['.$perm->id.']']) !!}
                            {!! Form::label('actions['.$perm->id.']', 'Actions') !!}
                        </div>
                    </div>
                @endforeach
                {!! Form::button('<i class="material-icons right">send</i> Asignar Permisos', ['class' => 'btn waves-effect waves-light deep-orange lighten-2 white-text', 'name' => 'action', 'type' => 'submit']) !!}
                {!! Form::close() !!}
            @else
                @include('no_records')
            @endif
        </div>

As you can see, for each permission set there is a checkbox for the perm->id named:

permission_id['.$perm->id.']

and same for the actions it has:

'actions['.$perm->id.']'

Everything goes fine since it is send to the controller as:

role_id:1 permission_id[1]:1 actions[1]:view, update, store, destroy permission_id[6]:6 actions[6]:create, destroy, deploy, fix, new

My controller iterate over each permission_id taken received from the array of fields named permission_id['.$perm->id.']. For each of those, a variable (array) is filled with the actions related to each permission, lets look to my method to attach the permissions to the role:

public function addPermissions(Request $request)
{
    // find the given role
    $role = Role::findOrFail($request->role_id);
    // foreach permission set, create the array to save the relation and the pivot table attribute
    foreach($request->permission_id as $perm)
    {
        // explode and serialize the actions string
        $permissions[$perm] = [
                'actions' => serialize(explode(',', str_replace(' ', '', $request->input('actions.'.$perm))))
        ];
    }
    // attach the given permissions to the given role
    $role->permissions()->attach($permissions);
    // redirect to the role details page
    return redirect('admin/roles/'.$role->id.'/#permissions');
}

The controller handle it pretty good, but the QUESTION IS, how can i handle validation for each permission set, since the name changes with each one?

Thank you very much <3 Laravel and Laracasts!

0 likes
2 replies
XSkinner's avatar

Thank you @coffee :) I think it is perfect so far.

Vye the way... how can you be an "I'm forever supporter of Laracasts"?

:D

Please or to participate in this conversation.