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
2
Answers
The issue I see is in this:
In your code:
And in your truncated console logs the first time around:
In your truncated console logs the second time:
Note that on the first pass there is only the one log of times, where the second pass has 2 separate logs of time, once the string, and once the Date object.
Then look back and notice that on the first pass, the
Departure:
line has nothing after it. There is nodeparture
time. Therefore, your checkif (arrival && departure)
is coming back false, so you’re never hitting the next line, which is your input validation.You also don’t have an
else
after thatif
, so the first pass, your code runs as:You can use validation on the Interactive grid, you can use pl/sql to compare the date columns and return an error message.
The validation will fire when saving the IG.
If this approach interests you, let me know I can provide an example.