Validation does not work with nested array input?

I have an array like this:

langs => [0=>'en',1=>'le'];

I have Foreach of langs with input like this:

<form wire:submit.prevent="store">

    @foreach ($langs as $index => $lang)
        <div wire:key="languages-{{ $index }}">
            <label>{{ $lang }}</label>
            <input type="text" wire:model="name.{{ $lang }}" />
            @error('name.'.$lang) <span class="error">{{ $message }}</span> @enderror
        </div>
    @endforeach

    <button type="submit" class="btn py-2 px-4 mt-7">
        @lang('site.save')
    </button>
</form>

Livewire component:

 public $name = [];

    protected $rules = [
        'name.*' => 'required|string|min:5|max:100',
    ];

    public function store()
    {
        $this->validate();

        dd('sdf');
  }

The validation does not work! and just pop up the dd that I have!!

Am I forget somethinng!?

I don’t see any issues, it could be livewire’s custom validation isn’t covering this type of array. Do a quick test like this and see if it works:

$validator = Validator::make($this->name, $this->rules)->validate();

Maybe try something like this:

public function mount()
{
   $this->name[] = collect($langs)->keys();
}

Take a look at Binding Directly To Model Properties here: https://laravel-livewire.com/docs/2.x/properties

You may want to setup your wire:model like this:
wire:model="name.{{ $index }}.{{ $lang }}"

or potentially without the {{ $lang }}

1 Like