Remote Validation for Date Fields in Meta Box AIO

Support Meta Box AIO Remote Validation for Date Fields in Meta Box AIO

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #47384
    Simon EgleSimon Egle
    Participant

    I have created two datetime fields: Screenshot of fields.

    I want to set up a validation rule to ensure that the end date cannot be earlier than the start date. To achieve this, I selected the “Remote” validation option for the end date field and provided the following URL:
    https://glossar:8890/wp-admin/admin-ajax.php?action=validate_end_date,
    which should trigger a function to validate the fields. However, it is not working as expected.

    Since I am new to this, I wanted to ask if I am going about this the wrong way. How should I properly set up this validation? I also tried using the “Max Value” option and entered the start date field there, but that didn’t work either.

    Any guidance would be greatly appreciated!

    add_action('wp_ajax_validate_end_date', function () {
        // Überprüfen, ob die Felder gesendet wurden
        if (isset($_GET['veranstaltung_start_wdjv6gfd912']) && isset($_GET['veranstaltung_ende_1j8y62g4ts'])) {
            $start_date = (int) $_GET['veranstaltung_start_wdjv6gfd912']; // Als Timestamp
            $end_date = (int) $_GET['veranstaltung_ende_1j8y62g4ts']; // Als Timestamp
    
            // Prüfen, ob das Enddatum vor oder gleich dem Startdatum liegt
            if ($end_date > $start_date) {
                echo 'true'; // Valid
            } else {
                echo 'false'; // Invalid
            }
        } else {
            echo 'false'; // Fallback, falls Werte fehlen
        }
        wp_die();
    });

    Thanks!

    #47394
    PeterPeter
    Moderator

    Hello Simon,

    Currently, the remote validation callback function only supports accessing one field value that you use the validation rule. It doesn't support accessing other field values in the callback function so you won't have value to compare dates like that.
    If you want to compare 2 field values on live, you should create some custom Javascript code and validate the field value before saving it.

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.