Search code examples
laravel-livewire

What does onUpdate: false parameter to the #[Validate] attribute in Livewire do?


I have the below in my code and having the onUpdate method to false has no different effect on the fronted when I submit a form. What does setting the parameter to false do?

#[Validate('required | image | max:1024', onUpdate: false)]


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

       //Logic to create a post
    }

Solution

  • The parameter onUpdate set to false disables automatic validation and the properties must be validated explicitly using $this->validate() or $this->validateOnly()

    For example:

    <div>
    
        <div>
    
            <input type="text" wire:model.blur="input1" placeholder="At least 5 chars">
    
            @error('input1')
                <div class="text-red-600">
                    {{ $message }}
                </div>
            @enderror
    
        </div>
    
        <div>
            <button wire:click="doSubmit">
                [ Submit! ]
            </button>
        </div>
    
    </div>
    
    namespace App\Livewire;
    
    use Livewire\Attributes\Validate;
    use Livewire\Component;
    
    
    class ValidateTest extends Component
    {
    
        #[Validate('required|min:5', onUpdate: false)]
        public $input1 = '';
    
    
        public function doSubmit()
        {
            // if onUpdate was true (default) the following line wouldn't be necessary
            $this->validate();
    
            // -- here we can do something like insert or update
        }
    
    
        public function updated($inputName)
        {
            // -- since onUpdate is false, to allow the validation on blur we need to enable the following line
            // $this->validateOnly($inputName);
        }
    }