Search code examples
laraveldatepickerlaravel-livewireflowbitelaravel-livewire-wiremodel

Laravel Livewire not mounting value from datepicker


I am using Flowbite Datepick plugin with my Livewire and Livewire Component is not mounting value of the datepicker and is NULL upon selecting date

                <div class="relative max-w-sm">
                    <label class="form-label mb-2 text-sm font-medium text-gray-900 dark:text-white" for="date_filed">{{ trans('cruds.leaveApplication.fields.date_filed') }}</label>
                    <div class="absolute py-2.5 left-0 flex items-center pl-3 pointer-events-none">
                      <svg aria-hidden="true" class="w-5 h-5 text-gray-500 dark:text-gray-400" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M6 2a1 1 0 00-1 1v1H4a2 2 0 00-2 2v10a2 2 0 002 2h12a2 2 0 002-2V6a2 2 0 00-2-2h-1V3a1 1 0 10-2 0v1H7V3a1 1 0 00-1-1zm0 5a1 1 0 000 2h8a1 1 0 100-2H6z" clip-rule="evenodd"></path></svg>
                    </div>
                    <input datepicker datepicker-autohide type="text" wire:model="leaveApplication.date_filed" id="date_filed" name="date_filed" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full pl-10 p-2.5  dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" placeholder="Select date">
                    <div class="validation-message">
                        {{ $errors->first('leaveApplication.date_filed') }}
                    </div>
                </div>

Solution

  • I solved this problem in this way.

    <div class="relative max-w-sm">
        <div class="relative max-w-sm">
            <div class="absolute inset-y-0 left-0 flex items-center pl-3 pointer-events-none">
                <svg aria-hidden="true" class="w-5 h-5 text-gray-500 dark:text-gray-400"
                    fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
                    <path fill-rule="evenodd"
                        d="M6 2a1 1 0 00-1 1v1H4a2 2 0 00-2 2v10a2 2 0 002 2h12a2 2 0 002-2V6a2 2 0 00-2-2h-1V3a1 1 0 10-2 0v1H7V3a1 1 0 00-1-1zm0 5a1 1 0 000 2h8a1 1 0 100-2H6z"
                        clip-rule="evenodd"></path>
                </svg>
            </div>
            <input datepicker datepicker-buttons datepicker-autohide type="text" id="date"
                datepicker-format="yyyy-mm-dd"
                class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full pl-10 p-2.5  dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
                placeholder="Select date">
        </div>
        <script>
            const datepickerEl = document.getElementById('date');
    
            datepickerEl.addEventListener('changeDate', (event) => {
                window.livewire.emit('dateSelected', event.detail.date);
            });
        </script>
    </div>
    

    In the script I emit of function updateDate of Livewire using listeners which are Livewire own

    This is a example

    protected $listeners = ['dateSelected' => 'updateDate'];
    
    public function updateDate($date)
    {
        $this->date = $date;
    }
    

    In this shape I get the value of the input of datepicker