? ? Pending
Pull Request for # 8646

User tests: Successful: Unsuccessful:

avatar joomdonation
joomdonation
12 Dec 2015

Problem

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

Solution

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

Testing instructions

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

Votes

# of Users Experiencing Issue
1/1
Average Importance Score
4.00

avatar joomdonation joomdonation - open - 12 Dec 2015
avatar joomdonation joomdonation - change - 12 Dec 2015
Status New Pending
avatar joomla-cms-bot joomla-cms-bot - change - 12 Dec 2015
Labels Added: ? ?
avatar joomla-cms-bot joomla-cms-bot - change - 12 Dec 2015
Labels Added: ? ?
avatar Fedik
Fedik - comment - 12 Dec 2015

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" :smile:

DateTime::__construct() [datetime.--construct]: can be esialy fixed with try/catch http://php.net/manual/en/language.exceptions.php

avatar Bakual
Bakual - comment - 12 Dec 2015

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.

avatar joomdonation
joomdonation - comment - 12 Dec 2015

@Bakual I think your comment is not correct. strtotime doesn't require the value in YYYY-MM-DD format. For example, this simple command does work echo date('d-m-Y', strtotime('20.11.1984'));

avatar Bakual
Bakual - comment - 12 Dec 2015

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

avatar joomdonation
joomdonation - comment - 12 Dec 2015

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

avatar joomdonation
joomdonation - comment - 12 Dec 2015

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.

avatar Bakual
Bakual - comment - 12 Dec 2015

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.

avatar Bakual
Bakual - comment - 12 Dec 2015

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.

avatar joomdonation
joomdonation - comment - 12 Dec 2015

@Bakual OK, So I mis-undertood the format parameter. If so, the code could be simpler, just simple use try / catch as @Fedik suggested. I will update the code.

avatar Bakual
Bakual - comment - 12 Dec 2015

Yeah, I think that try/catch should work fine.

avatar brianteeman brianteeman - change - 28 Jan 2016
Rel_Number 0 8646
Relation Type Pull Request for
Labels
avatar brianteeman brianteeman - change - 10 Mar 2016
Category Fields
avatar brianteeman brianteeman - change - 27 Apr 2016
Category Fields Fields Language & Strings
avatar brianteeman brianteeman - test_item - 8 May 2016 - Tested unsuccessfully
avatar brianteeman
brianteeman - comment - 8 May 2016

I have tested this item :red_circle: 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


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/8656.

avatar Edu20 Edu20 - test_item - 1 Aug 2016 - Tested successfully
avatar Edu20
Edu20 - comment - 1 Aug 2016

I have tested this item βœ… successfully on 8c6b481

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


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/8656.

avatar wmchris wmchris - test_item - 1 Aug 2016 - Tested successfully
avatar wmchris
wmchris - comment - 1 Aug 2016

I have tested this item βœ… successfully on 8c6b481

Works as described @icampus

Value 201608-30 12:9:16 you entered for Start Publishing is invalid datetime format


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/8656.

avatar dgt41
dgt41 - comment - 1 Aug 2016

@joomdonation #11138 should be a better solution (although that will be client side as your approach here is server side)

avatar joomdonation
joomdonation - comment - 1 Aug 2016

@dgt41 Haven't looked at your PR carefully yet but does your PR prevents users from entering date in wrong format (manually) ?

avatar dgt41
dgt41 - comment - 1 Aug 2016

@joomdonation yes, the field is readonly and the changes are made only through the popup calendar (which is also accessible)

avatar joomdonation
joomdonation - comment - 1 Aug 2016

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

avatar joomdonation joomdonation - change - 1 Aug 2016
Status Pending Closed
Closed_Date 0000-00-00 00:00:00 2016-08-01 11:11:03
Closed_By joomdonation
avatar brianteeman brianteeman - change - 1 Aug 2016
Status New Closed
Closed_Date 2016-08-01 11:11:03 2016-08-01 11:30:02
Closed_By joomdonation brianteeman
Labels

Add a Comment

Login with GitHub to post a comment