Hello fellow Trongate users,
I have created the following validation helper function using the callback_ method in the validation rule for the date_arrived field to ensure date input is not a future date.
However, this does not work, as no error message is returned if the date input is a future date.
Can anyone suggest a better way to go about this?
Cheers!
Custom date validation helper function
4 years ago
4 years ago
#1
4 years ago
#2
I have found a workaround for this,
Instead of using a validation rule, min and max dates could be set directly as one the the attributes for the date input field.
example below:
For this to work as expected, you must set the correct date.timezone value in your php.ini file (there are 2 entries for this) and
you should restart apache after saving changes to the file.
Note: I am using the native html5 datepicker input to enter dates instead of the date-picker.js that comes with Trongate, since it it easier to select different years with the native datepicker.
I still would like to find out why my custom validation code doesn't work though...
Cheers!
Instead of using a validation rule, min and max dates could be set directly as one the the attributes for the date input field.
example below:
For this to work as expected, you must set the correct date.timezone value in your php.ini file (there are 2 entries for this) and
you should restart apache after saving changes to the file.
Note: I am using the native html5 datepicker input to enter dates instead of the date-picker.js that comes with Trongate, since it it easier to select different years with the native datepicker.
I still would like to find out why my custom validation code doesn't work though...
Cheers!
4 years ago
#3
Hello,
I think you can use the greater than in Validation Rules Reference, in the Docs.
https://trongate.io/docs_m/information/the-form-validation-helper
Use timestamp of the date_arrived , then compare that to time(now).
Dan
I think you can use the greater than in Validation Rules Reference, in the Docs.
https://trongate.io/docs_m/information/the-form-validation-helper
Use timestamp of the date_arrived , then compare that to time(now).
Dan
4 years ago
#4
Hi djnordeen,
Thanks for your response, I have been a bit wary of the Unix timestamp since reading up on it's limitations.
You would notice that in my code I tried to convert the dates to DateTime objects and then compare them, my issue with the unix timestamp approach is that it does not take my locale into consideration, defaulting to UTC.
Is there a way to get the timestamp to adhere to my timezone (America/Port_of_Spain)?
Cheers!
Thanks for your response, I have been a bit wary of the Unix timestamp since reading up on it's limitations.
You would notice that in my code I tried to convert the dates to DateTime objects and then compare them, my issue with the unix timestamp approach is that it does not take my locale into consideration, defaulting to UTC.
Is there a way to get the timestamp to adhere to my timezone (America/Port_of_Spain)?
Cheers!
4 years ago
#5
Hi KeepCalm,
You can use the following, I'll show you my zone in Melbourne and yours:
which outputs:
I have a few things to do, but will look above as to why your callback_ is not working
You can use the following, I'll show you my zone in Melbourne and yours:
which outputs:
I have a few things to do, but will look above as to why your callback_ is not working
4 years ago
#6
Just had another look at your callback_check_future_date($date_arrived) function and DateTime() which returns an object and kinda does my head in once I started to read the docs on it > https://www.php.net/manual/en/class.datetime.php
The main issue is you can't just compare two objects, you have to drill into the elements of the array. Also, your method to get the current date is a little flawed.
I modified your function so I could test it via the URL
I tested it with this url > http://localhost/help_bar/e/check_future_date/2022-09-03T15:03:01.012345
which is just a random future date (9th Sep 2022 3:15:01:012345 pm) notice the use of micro seconds (kinda cool)
output is this:
Reading through the docs, you could perform all sorts of shenanigans on it, but for a simple date compare, I'd rather change it to this, but you may want to stick with DateTime() :
url test > http://localhost/help_bar/e/check_future_date/2022-09-03T15:03:01
url test > http://localhost/help_bar/e/check_future_date/2022-07-03T15:03:01
Having established that, we can put your function back the way it was, but using my method - I'm not so sure about pulling the date_arrived from post or pass it to the callback_ as a parameter, you may have to experiment a bit here:
The main issue is you can't just compare two objects, you have to drill into the elements of the array. Also, your method to get the current date is a little flawed.
I modified your function so I could test it via the URL
I tested it with this url > http://localhost/help_bar/e/check_future_date/2022-09-03T15:03:01.012345
which is just a random future date (9th Sep 2022 3:15:01:012345 pm) notice the use of micro seconds (kinda cool)
output is this:
Reading through the docs, you could perform all sorts of shenanigans on it, but for a simple date compare, I'd rather change it to this, but you may want to stick with DateTime() :
url test > http://localhost/help_bar/e/check_future_date/2022-09-03T15:03:01
url test > http://localhost/help_bar/e/check_future_date/2022-07-03T15:03:01
Having established that, we can put your function back the way it was, but using my method - I'm not so sure about pulling the date_arrived from post or pass it to the callback_ as a parameter, you may have to experiment a bit here:
4 years ago
#7
Hi DaFa,
Your code works flawlessly.
Thank you very much!
One quick question, how would I pass the current validation rule's date value to the callback function?
I have six dates to test on the same form and I would like to reuse this function to test them all.
Edit: Nevermind the last question, I figured this one out.
Cheers!
Your code works flawlessly.
Thank you very much!
One quick question, how would I pass the current validation rule's date value to the callback function?
I have six dates to test on the same form and I would like to reuse this function to test them all.
Edit: Nevermind the last question, I figured this one out.
Cheers!