Oracle APEX validation in interactive grid

I am trying to validate two date columns in interactive grid with Javascript and use of DA.

I am currently “stuck” at a point where the code works, but it also doesn’t. What it’s supposed to do, is to check two date columns if the e.g. departure date is later than arrival date. If the departure date is earlier than arrival date, then it should return an error that the date is incorrect. Now I can freely pick an incorrect departure date and save it, but when I go and edit the departure date and change the date, that’s when the error pops up. I think there’s an issue with validation because in the console I can see that the function is being called but the log for checking record only registers arrival date being picked but not departure, where the validation should be made.

There’s no actual error. The “error” is that when I first add the date, I can save it without a problem, even if the date is INCORRECT console log (that’s when I should get a pop up message saying the date is incorrect). And then when I go and edit the date, I get a console log, that the previous date was recognized and that’s when the error pops up. As if the validation was delayed by one date

How could I fix that or does anyone know, where/what could the problem be?

function validateDate() {
    console.log("Validation function called");

    var regionStaticId = "my_ig";
    var grid;

    try {
        grid = apex.region(regionStaticId).widget().interactiveGrid("getViews", "grid");
    } catch (e) {
        console.error("Interactive grid not found.", e);
        return false;
    }

    var model = grid.model;
    var arrivalIdx = model.getFieldKey("Arrival");
    var departureIdx = model.getFieldKey("Departure");

    apex.message.clearErrors();

    var currentRecord = grid.view$.grid("getSelectedRecords")[0];

    if (currentRecord) {
        var arrival = currentRecord[arrivalIdx];
        var departure = currentRecord[departureIdx];

        console.log("Checking record: ", currentRecord);
        console.log("Arrival: ",arrival, "Departure: ",departure);

        if (arrival && departure) {
            var arrivalDate = new Date(arrival);
            var departureDate = new Date(departure);

            console.log("Arrival Date: ",arrivalDate, "Departure Date: ",departureDate);

            if (arrivalDate < departureDate) {
                console.log("Invalid date detected");

                apex.message.showErrors([{
                    type: "error",
                    location: "page",
                    message: "Pick a different date.",
                    pageItem: null
                }]);

                grid.view$.grid("gotoCell", {
                    row: model.getRecordId(currentRecord),
                    field: departureIdx
                });

                model.setValue(currentRecord, departureIdx, null);
                return false; 
            }
        }
    } else {
        console.error("No editable record selected.");
    }

    return true; 
}

$(document).ready(function() {
    $("input[name=fo6]").on("change", function(event) {
        if (!validateDate()) {
            event.preventDefault();
        }
    });
});

IN DYNAMIC ACTION I CALL THE FUNCTION ABOVE