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

jmacdiarmid's avatar

Laravel 8 Livewire array group validation

I have a list of roles and at least 1 needs to be checked.

  • SuperAdmin
  • Admin
  • CEO
  • CFO
  • Manager
  • Sales
  • Client
  • Consultant

Here's my code:

 public array $selectedRoles = [] ;

 'selectedRoles'         => 'required|array|string|min:1',

I'm currently getting the error "The selected roles must be a string." and I have the value set to the name of the role I'm not sure what it's talking about.

Additionally, I would like to have:

  • Array group required when creating a new user
  • Not required when editing an existing user

I tagged input checkbox as required and when looping over the set of roles, all of the input elements are marked as required.

                    <fieldset name="roles"  {{ $userId ? '' : 'required' }}>

                    <legend class="block font-semibold text-sm text-gray-700 mt-2">
							Roles (Multiple Roles May Be Selected)
					</legend>

                    <div class="grid grid-cols-4 gap-2">

                        @foreach($roles as $id => $role)

                        <div class="mt-1">

                            <label for="selectedRoles" class="inline-flex items-start">
                                <input wire:model.lazy="selectedRoles.{{ $id }}" 
									id="selectedRoles" 
									name="selectedRoles[]" 
									type="checkbox"  
									class="form-checkbox" 
									value="{{ $role->name }}"
                                    autocomplete="selectedRoles" {{ in_array($role, $selectedRoles, true) ? 'checked' : ''  }}>
                                <span class="ml-3 text-sm">{{ $role->name }}</span>
                            </label>

                        </div>

                        @endforeach
                   </div>
                   </fieldset>	
0 likes
1 reply
jmacdiarmid's avatar

I think I have all of this working for the most part. But I'm still a bit confused about how the implicit validation rules work, where attributes that are not present or have an empty value (documentation mentions "string") normal and custom validation rules are not run. Additionally, for this to work, all fields need to be required. https://laravel.com/docs/8.x/validation#implicit-rules Am I missing something?

My rules are currently stated thusly:

        return [

            'name'                   => 'required|string|min:3',

            'email'                   => $this->userId 
                                          ?  ['required', 'email', Rule::unique('users')->ignore($this->userId)]
                                          :  'required|email|unique:users,email',

            'phone'                   => 'required|digits:11',

            'password'             => ['required', 'confirmed', Password::defaults()],

            'selectedRoles'      => 'required|array|min:1',

            'selectedRoles.*'   => 'required|string|distinct|min:1',

        ];

In my Store function:

            $rules = $this->rules();

            $input = [
                'password'  => '',
                'password_confirmation' => ''
            ];

            Validator::make($input, $rules)->passes();

            $validatedData = $this->validate($this->rules());

            $user = User::updateOrcreate([
                'id' => $this->userId
            ],[
                'name'              => $validatedData['name'],
                'email'             => $validatedData['email'],
                'phone'             => $validatedData['phone'],
                'password'          => Hash::make($validatedData['password']),
            ]);

            $user->assignRole($validatedData['selectedRoles']);

            $updateOrCreateVerb = ($this->userId) ? 'updated' : 'created';

            $this->success = "User {$user->name} was {$updateOrCreateVerb} successfully!";

Please or to participate in this conversation.