User tests: Successful: Unsuccessful:
Currently, when you enter an invalid datetime value for a calendar form field, Joomla will throw a system error like this
DateTime::__construct() [datetime.--construct]: Failed to parse time string (jerror) ......
There were atleast two issues reported #8646 and #8093 which I can see
I attempt to fix this issue by introducing a new form rule JFormRuleDatetime which uses strtotime($value) === false command to validate the value of the field. If it return false, the date is invalid and an exception will be returned
With this PR, I just modify the add/edit article form in the backend of Joomla to show how it works.
Please try to edit an article, before this patch, if you enter a wrong value for any Calendar field (for example Start Publishing, Finish Publishing.., you will see a system error
After this patch, there will be an error message like this being showed and it will prevent you from saving the article
Warning
Value 2015-13-30 19:07:12 you entered for Start Publishing is invalid datetime format
Status | New | ⇒ | Pending |
Labels |
Added:
?
?
|
Labels |
Added:
?
?
|
strtotime
requires an english date (YYYY-MM-DD) while the calendar field actually would accept any date that PHP can detect. Which for example may also be the german format DD.MM.YYYY.
Interesting, because the PHP manual says otherwise: http://php.net/manual/en/function.strtotime.php
strtotime β Parse about any English textual datetime description into a Unix timestamp
There are many formats accepted http://php.net/manual/en/datetime.formats.php. I think Joomla core JHtml::calendar uses the same code, too https://github.com/joomla/joomla-cms/blob/staging/libraries/cms/html/html.php#L982
I improve the code a bit to check and make sure the value not only a valid date Datetime string but also have to meet the format defined by format attribute of the form field.
JHtml::calendar uses the same code, too
That one is actually when it will be displayed. At this time, $value always is in YYYY-MM-DD format as it is coming from the database. So it's not the same case.
But if it works with other formats as well, then it should be fine.
I improve the code a bit to check and make sure the value not only a valid date Datetime string but also have to meet the format defined by format attribute of the form field.
That would be wrong. The format parameter is meant to specify how the date is displayed. How it is entered doesn't matter at all as long as PHP can detect it. So you would enforce more than is required.
Yeah, I think that try/catch should work fine.
Rel_Number | 0 | ⇒ | 8646 |
Relation Type | ⇒ | Pull Request for | |
Labels |
Category | ⇒ | Fields |
Category | Fields | ⇒ | Fields Language & Strings |
I have tested this item unsuccessfully on 8c6b481
I tested this by adding an invalid date in the published date field of a news feed. When pressing save I expected an error message but instead the item was saved but with no value in that field
I have tested this item
I have tested this patch successfully with the follow test-entries in the field(Finish Publishing):
Warning
Value 12.oo.2016 you entered for Finish Publishing is invalid datetime format
Warning
Value 30.##.2016 you entered for Finish Publishing is invalid datetime format
Warning
Value 30 06 2016 you entered for Finish Publishing is invalid datetime format
Warning
Value 34 14 2016 you entered for Finish Publishing is invalid datetime format
Warning
Value 2020 ΓΌ 1f you entered for Finish Publishing is invalid datetime format
Tested @icampus
I have tested this item
Works as described @icampus
Value 201608-30 12:9:16 you entered for Start Publishing is invalid datetime format
@joomdonation #11138 should be a better solution (although that will be client side as your approach here is server side)
@joomdonation yes, the field is readonly and the changes are made only through the popup calendar (which is also accessible)
OK. Then in this case, I will close this PR (not really happy with the code on this PR too).
Thanks everyone for feedback and testing
Status | Pending | ⇒ | Closed |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2016-08-01 11:11:03 |
Closed_By | ⇒ | joomdonation |
Status | New | ⇒ | Closed |
Closed_Date | 2016-08-01 11:11:03 | ⇒ | 2016-08-01 11:30:02 |
Closed_By | joomdonation | ⇒ | brianteeman |
Labels |
I would stay away from
strtotime
it has limit to 2038 year, and retun unexpected results for the dates that does not fit to "Unix epoch"DateTime::__construct() [datetime.--construct]:
can be esialy fixed withtry/catch
http://php.net/manual/en/language.exceptions.php