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

ToxifiedM's avatar

Unable to ignore email of the user, when updating a selected user, in case of model property binding, in Livewire.

Description

As I'm implying model property binding, by type hinting. I am unable to ignore the email of the user selected to edit, when updating user details from the admin panel. I am using the $rules property, to validated the rules.

Exact steps to reproduce

Whenever I am trying to add the rule to the email input to ignore the email of the selected user, it gives a syntax error.

    protected $rules = [
        'user.name' => ['required','string','max:255'],
        'user.email' => ['required','string','email','max:255','unique:users,email'.$user->id],
        'role' => ['required'],
    ];

Stripped-down, copy-pastable code snippets

App\Http\Livewire\UserController

    public $role;
    public User $user;

    protected $rules = [
        'user.name' => ['required','string','max:255'],
        'user.email' => ['required','string','email','max:255','unique:users,email'],
        'role' => ['required'],
    ];

    public function mount()
    {
        $this->user = $this->resetUserForm();
    }

    public function resetUserForm()
    {
        return User::make();
    }

resources\views\livewire\user-controller

    <form wire:submit.prevent="save">
        <x-modal.dialog wire:model.defer="showEditModal">
            <x-slot name="title">Edit User</x-slot>

            <x-slot name="content">
                <x-input.group for="name" label="Name" :error="$errors->first('user.name')">
                    <x-input.text wire:model="user.name" id="name" placeholder="Enter Name" />
                </x-input.group>

                <x-input.group for="email" label="Email" :error="$errors->first('user.email')">
                    <x-input.text wire:model="user.email" id="email" placeholder="Enter Email" />
                </x-input.group>

                <x-input.group for="role" label="Role" :error="$errors->first('role')">
                    <x-input.select wire:model="role" id="role">
                        @foreach ($roles as $role)
                            <option value="{{ $role->id }}">{{ $role->name }}</option>
                        @endforeach
                    </x-input.select>
                </x-input.group>

                <x-input.group for="delete" label="Delete">
                    <x-button.secondary wire:click="delete">Delete Account</x-button.secondary>
                </x-input.group>
            </x-slot>

            <x-slot name="footer">
                <x-button.secondary wire:click="$set('showEditModal', false)">Cancel</x-button.secondary>
                <x-button.primary type="submit">Save</x-button.primary>
            </x-slot>
        </x-modal.dialog>
    </form>

Context

  • Livewire version: 2.3.5
  • Laravel version: 8.17.2
  • Alpine version: 2.7.3
  • Browser: Chrome
0 likes
3 replies
tomasosho's avatar

You set your user in the rules as required, my advice is you should make user email input field as value not placeholder e.g

<x-input.group for="email" label="Email" :error="$errors->first('user.email')">
                    <x-input.text wire:model="user.email" id="email" value="{{$user->email}}" />

OR you take out the required from your rules under email

ToxifiedM's avatar

The placeholder is just showing the greyed out text asking the user to 'Enter Email'. The thing here is, livewire is all dynamic, which shows the value of the name and email of the user, all dynamically in the input field. So it's perfectly fine. And incase of removing 'required' from the email rule, it will be worst and glitchy, as the user can leave the email blank too, and that's not something I'm looking forward to.

tomasosho's avatar

Check your error log to confirm the error message produced

before your rule in your controller, you can say

if(email == ' ')
{
$email = $user->email
protected $rules = [
        'user.name' => ['required','string','max:255'],
        'user.email' => ['required','string','email','max:255','unique:users.email'],
        'role' => ['required'],
    ];
}

Please or to participate in this conversation.