Home  >  Q&A  >  body text

Livewire and flatpickr: changes when handling empty data

Disclaimer: I know there have been some questions about how to couple Livewire and flatpickr, but I don't understand the solutions provided because they are very different from how I approach the problem. Having said that, I'm still learning Livewire, so I might just be doing it wrong.

I have a Livewire component in which I use flatpickr to select a date and time.

<div class="mb-3" >
    <input id="chosendatetime" type="datetime-local" name="chosendatetime" value="{{ old('chosendatetime') }}" 
               wire:model.debounce.500ms="chosendatetime" />
</div>

In the blade component, I also have a script section for initializing flatpickr:

<script>
    flatpickr("#chosendatetime", {
        enableTime: true,
        dateFormat: "d-m-Y H:i",
    });
</script>

The date picker is rendered correctly, but when I change its value, the data sent by the client is empty.

what should I do?

P粉727416639P粉727416639175 days ago361

reply all(2)I'll reply

  • P粉131455722

    P粉1314557222024-04-01 16:06:46

    Your code runs fine if you set up Livewire correctly.

    • Make sure you have a public property named chosendatetime in your Livewire class public $chosendatetime;
    • Add a function called updatedChosenDatetime() and print $this->chosendatetime using dd() to see if the value was received
    • Make sure to include @livewireStyles and @livewireScripts
    • in your layout template
    • Make sure you have included FlatPickr’s JS and CSS

    The code of the Livewire class is as follows:

    <?php
    
    namespace App\Http\Livewire;
    
    use Livewire\Component;
    
    class FlatPickr extends Component
    {
        public $chosendatetime;
    
        public function updatedChosenDatetime()
        {
            dd($this->chosendatetime);
        }
    
        public function render()
        {
            return view('livewire.flat-pickr');
        }
    }

    reply
    0
  • P粉275883973

    P粉2758839732024-04-01 12:22:23

    Try adding wire:ignore to the div element like this:

    <div class="mb-3" wire:ignore>
        <input id="chosendatetime" type="datetime-local" name="chosendatetime" value="{{ old('chosendatetime') }}" 
                   wire:model.debounce.500ms="chosendatetime" />
    </div>

    This directive tells Livewire that this part of the page should be skipped and not changed when the component refreshes. If you don't use it, Livewire may replace the flatpickr instance and make it stop working.

    reply
    0
  • Cancelreply