Skip to content

Setting the startDate/minDate of the second date picker based on the value of the first one. #5802

@AdamovicBoban

Description

@AdamovicBoban

Bug report

What I did

CRUD::field('pickup_date')
            ->label('Pick Up Date')
            ->type('date_picker')
            ->date_picker_options([
                'startDate' => '+1d',
                'autoclose' => true,
                'todayHighlight' => true,
                'clearBtn' => true,
                'format' => 'dd.mm.yyyy',
                'language' => 'en',
            ])
            ->allows_null(false);

        CRUD::field('pickup_date_return')
            ->label('Pick Up Date - Return')
            ->type('date_picker')
            ->date_picker_options([
                'startDate' => '+1d',
                'autoclose' => true,
                'todayHighlight' => true,
                'clearBtn' => true,
                'format' => 'dd.mm.yyyy',
                'language' => 'en',
            ]);

JavaScript

  function updateReturnDateMin() {
            const pickupDateEl = $("input[name='pickup_date']");
            const returnDateEl = $("input[name='pickup_date_return']");

            const pickupDateVal = pickupDateEl.val();

            if (pickupDateVal) {

                const parts = pickupDateVal.split(".");
                const pickupDate = new Date(parts[2], parts[1] - 1, parts[0]);

                returnDateEl.datepicker("setStartDate", pickupDate);

                const returnDateVal = returnDateEl.val();
                if (returnDateVal) {
                    const returnParts = returnDateVal.split(".");
                    const returnDate = new Date(returnParts[2], returnParts[1] - 1, returnParts[0]);

                    if (returnDate < pickupDate) {
                        returnDateEl.datepicker("setDate", pickupDate);
                    }
                }
            }
        }

        $(document).on("changeDate", "input[name='pickup_date']", updateReturnDateMin);
        $(document).on("change", "input[name='pickup_date']", updateReturnDateMin);

What I expected to happen

I expect the second date picker to have its start date/min date limited based on the date value from the first one.

What happened

Nothing!

What I've already tried to fix it

I tried a million different combinations I found on Google and the ones GPT suggested.

Backpack, Laravel, PHP, DB version

When I run php artisan backpack:version the output is:

### PHP VERSION:
8.3.14

### PHP EXTENSIONS:
Core, bcmath, calendar, ctype, date, filter, hash, iconv, json, SPL, pcre, random, readline, Reflection, session, standard, mysqlnd, tokenizer, zlib, libxml, dom, PDO, openssl, SimpleXML, xml, xmlreader, xmlwriter, ldap, curl, com_dotnet, fileinfo, gd, gettext, gmp, intl, imap, mbstring, exif, mysqli, Phar, pdo_mysql, pdo_sqlite, soap, sockets, sqlite3, xsl, zip, Zend OPcache, xdebug

### LARAVEL VERSION:
12.5.0.0

### BACKPACK PACKAGE VERSIONS:
backpack/basset: 1.3.8
backpack/crud: 6.8.0
backpack/devtools: 3.1.7
backpack/editable-columns: 3.0.11
backpack/generators: v4.0.7
backpack/medialibrary-uploaders: 1.2.1
backpack/permissionmanager: 7.2.1
backpack/pro: 2.2.33
backpack/theme-tabler: 1.2.17

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions