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

nexxai's avatar
Level 37

`Call to a member function with() on null` when validating an array in Livewire v3

I have this array in a Livewire component:

use Livewire\Attributes\Rule;

...

#[Rule(['friends.*' => 'email'])]
public array $friends = [''];

and I update it like this:

@foreach($friends as $friend)
<div class="relative">
    <input type="text" id="friends" wire:model.live="friends.{{ $loop->index }}"
        placeholder="[email protected]"
        class="w-full py-4 pl-6 pr-12 text-lg rounded-lg focus:ring-purple-600 focus:ring-2 focus:ring-offset-4 focus:ring-offset-gray-100 focus:outline-none"
        autocomplete="off" />
    <div class="absolute right-4 top-5">
        <button
            class="ml-4 border-2 rounded-md hover:cursor-pointer border-slate-400 bg-slate-400/10 focus:ring-purple-600 focus:ring-2 focus:ring-offset-4 focus:ring-offset-white focus:outline-none"
            wire:click="removeFriend({{ $loop->index }})" aria-label="Remove friend">
            <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2"
                stroke="currentColor" class="w-4 h-4">
                <path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12" />
            </svg>
        </button>
    </div>
    <div>@error('friends.{{ $loop->index }}') {{ $message }} @enderror</div>
</div>
@endforeach

<button wire:click="addFriend"
    class="flex px-3 py-2 space-x-2 font-semibold border rounded-lg bg-gray-50 hover:bg-white hover:shadow-lg focus:ring-purple-600 focus:ring-4 focus:ring-offset-4 focus:bg-white focus:ring-offset-gray-100 focus:outline-none focus:shadow-lg">
    <span class="inline-block">Add friend</span>
    <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5"
        stroke="currentColor" class="w-6 h-6">
        <path stroke-linecap="round" stroke-linejoin="round" d="M12 4.5v15m7.5-7.5h-15" />
    </svg>
</button>

When I call $this->validate(); in my render() method and the validation fails, I get this error message:

https://flareapp.io/share/dmk3NGYm

The middleware that Flare/Ignition highlights is definitely not the problem, because as you can see, there are 14 vendor frames after that which seem to point to some missing piece in the validation, but I can't figure out what.

Can anyone see anything obviously wrong with what I'm doing? On the third-from-the-top stack call, it's passing $properties which it's trying to fetch from the Utils::getPublicPropertiesDefinedOnSubclass($component); method, but as you can see from my paste, the array is definitely marked as public, so I don't know what else I should be looking for.

0 likes
6 replies
tangtang's avatar

@nexxai

seems like you're using with() method inside the @foreach loop in your blade view. when validation fails, the error message is displayed for each friend's email input.

however, livewire does not support the use of the with() method for setting additional view data within a @foreach loop. instead, you should handle this logic within the livewire component itself.

this code as reference

use Livewire\Attributes\Rule;

class YourLivewireComponent extends Component
{
    public array $friends = [''];

    public function addFriend()
    {
        $this->friends[] = '';
    }

    public function removeFriend($index)
    {
        unset($this->friends[$index]);
        $this->friends = array_values($this->friends);
    }

    public function updatedFriends()
    {
        $this->validate([
            'friends.*' => Rule::email(),
        ]);
    }

    public function render()
    {
        return view('livewire.your-view');
    }
}
@foreach($friends as $index => $friend)
<div class="relative">
    <input type="text" id="friends" wire:model="friends.{{ $index }}"
        placeholder="[email protected]"
        class="w-full py-4 pl-6 pr-12 text-lg rounded-lg focus:ring-purple-600 focus:ring-2 focus:ring-offset-4 focus:ring-offset-gray-100 focus:outline-none"
        autocomplete="off" />
    <div class="absolute right-4 top-5">
        <button class="ml-4 border-2 rounded-md hover:cursor-pointer border-slate-400 bg-slate-400/10 focus:ring-purple-600 focus:ring-2 focus:ring-offset-4 focus:ring-offset-white focus:outline-none"
            wire:click="removeFriend({{ $index }})" aria-label="Remove friend">
            <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2"
                stroke="currentColor" class="w-4 h-4">
                <path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12" />
            </svg>
        </button>
    </div>
    <div>@error('friends.' . $index) {{ $message }} @enderror</div>
</div>
@endforeach
nexxai's avatar
Level 37

@tangtang I am definitely not using the ->with() method inside of the component; the null with() it is referring to is the empty with() on line 134 inside the \Livewire\Drawer\Utils class that is part of the Livewire package.

You can see the entire @foreach loop in my original post and you can see nothing I'm doing there even so much as accesses a Laravel model. That is not a contrived example I made for this help post, that is literally copy/pasted verbatim from my code.

If you look at the stack trace and expand the vendor frames above the highlighted code, it's the second stack frame at the top.

nexxai's avatar
nexxai
OP
Best Answer
Level 37

Ok I have no idea why this works but I moved the $this->validate(); call to a separate public function updatedFriends() and it seems to have solved my issue.

I have no idea why that makes a difference, but it works and I'm satisfied.

CyberList's avatar

Same problem on my side, I have around ten fields that I want to validate directly without clicking on a submit to sort data in a table next to the form. I do not know how to do.

brutalhost's avatar

The problem is still relevant. I encounter it when I try to validate inputs from the Livewire Form class both in this class itself and in the component where it is embedded.

antiandrogen's avatar

I have this exact same issue, it seems to be related to the validation code. I also get exclusively vendor frames in the stack trace.

Please or to participate in this conversation.