Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/3574 date field doesnt work when date is entered manually #3724

Conversation

tramuntanal
Copy link
Contributor

@tramuntanal tramuntanal commented Jun 27, 2018

🎩 What? Why?

Remove the readonly attribute from date fields and add a text help with the localized format to be used by the user to enter the date manually.

📌 Related Issues

📋 Subtasks

  • Add CHANGELOG entry
  • [-] Add documentation regarding the feature
  • [-] Add/modify seeds
  • Add tests

📷 Screenshots (optional)

Description

@tramuntanal tramuntanal self-assigned this Jun 27, 2018
@ghost ghost added the status: WIP label Jun 27, 2018
@tramuntanal
Copy link
Contributor Author

Hi @decidim/lot-core , I need to know why dates in english don't use the standard mm/dd/yyyy format. It is a problem to make the datepicker and the FormObjects to use the same parse formats.

@mrcasals
Copy link
Contributor

mrcasals commented Jul 5, 2018

@tramuntanal what format are they using?

@tramuntanal
Copy link
Contributor Author

tramuntanal commented Jul 6, 2018

I think that FormObjects are picking en.date.formats.decidim_short: "%d/%m/%Y", but I can't find were it is configured.

@mrcasals
Copy link
Contributor

mrcasals commented Jul 9, 2018

@tramuntanal a quick search gives me this:

decidim_short: "%d/%m/%Y"

decidim_short: "%d/%m/%Y %H:%M"

@tramuntanal
Copy link
Contributor Author

Hi @decidim/lot-core ,
Sorry I guess I did not explain myself correctly. I have two questions:

  • Why English locales use d/m/y format when it should be m/d/y?
  • How is that FormObjects use d/m/y format? were in the code are FormObject configured to use this format?

@mrcasals
Copy link
Contributor

mrcasals commented Jul 9, 2018

Oh, I misunderstood the problem LOL sorry 😅

For English locales: Maybe it's a typo? Or maybe I did it intentionally (I always thought m/d/y makes zero sense, but who am I to judge?). Let's consider this a typo 😂

For the FormObjects: let me check it first, but maybe it's just using a Date.new(string) and tries to deduce the format

@tramuntanal
Copy link
Contributor Author

FormObjects use Virtus coercion system which is not documented but it seems to use Date.parse for date attributes. Date.parse is also scarcely documented although the "expected" correct format seems to be 'yyyy-mm-dd'. After some stackoverflowing it seems that Date.parse works by parsing by guess, this is why Decidim works with 'dd/mm/yyyy'. Doing exploration through the rails console one can see that it correctly resolves dates in the format 'dd/mm/yyyy' but fails to parse the native english 'mm/dd/yyyy' format, impeding the parsing of localized dates (dates in all Decidim's supported locales).

irb(main):029:0> I18n.locale
=> :en
irb(main):032:0> Date.parse('09/27/2017')
Traceback (most recent call last):
        2: from (irb):32
        1: from (irb):32:in `parse'
ArgumentError (invalid date)
irb(main):033:0> Date.parse('27/09/2017')
=> Wed, 27 Sep 2017
irb(main):036:0> Date.parse('2017-09-27')
=> Wed, 27 Sep 2017

Summarizing, as we use Rectify, and thus Virtus, which rely on Date.parse for parsing dates, the best will be to always force the user to enter dates in the 'yyyy-mm-dd' format, or alternatively use a most common and also supported 'dd/mm/yyyy' format. This will affect users entering dates manually whatever is their locale and rendered dates (datepickers will remain the same as now).

cc/ @decidim/lot-core @decidim/product @decidim/lot-px any thoughts on this?

@oriolgual
Copy link
Contributor

Actually, mm/dd/yyyy is mostly used at USA, so it isn't that bad to use dd/mm/yyyy (which makes more sense to me 😅).

…as it is used also in decidim-verificators, and may be used in any public view.
@tramuntanal
Copy link
Contributor Author

tramuntanal commented Jul 13, 2018

Hi @decidim/lot-core ,
I have to do the refactor in this commit in order for the datepicker to work in accordance with manual date entrering.
Also, I noticed that the datepicker format is declared in decidim-admin but datepicker also appears in decidim-verifications public part and in the future may appear in any public view so I think it must belong to core.
The question is how should I proceed to move the keys from decidim-admin to decidim-core? is this PR ok? If not, which is the procedure?

(This PR is still WIP)

@mrcasals
Copy link
Contributor

@tramuntanal yes, it's OK if you do it in this PR! 😄

@tramuntanal tramuntanal force-pushed the fix/3574-date_field_doesnt_work_when_date_is_entered_manually branch from 8ba234f to f224407 Compare July 17, 2018 16:19
mrcasals
mrcasals previously approved these changes Aug 6, 2018
@deivid-rodriguez
Copy link
Contributor

I removed some i18n keys that are now unused.

That makes me wonder if we should actually keep them. I originally suggested the removal in #3724 (comment), but I'm now not sure about it. Previously, the formatted dates from pickers could be customized to use their own formatting, now they use decidim_short, so if you change that formatting, it will change all over the application (not only on pickers). I'm not sure if this configurability is actually needed, but restoring the old way would make the diff in the this patch smaller.

Thoughts?

@deivid-rodriguez
Copy link
Contributor

Nah, I think it's fine to leave it as it is.

@mrcasals
Copy link
Contributor

mrcasals commented Aug 7, 2018

@deivid-rodriguez I'll wait for @tramuntanal's opinion and, if he thinks everything looks good, I'll merge this 😄

return value unless value.is_a?(String)
Date.strptime(value, I18n.t("date.formats.decidim_short"))
rescue ArgumentError
nil
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very clever @deivid-rodriguez !
Creating a custom attribute coercer is much better design than overriding coercible.
Just a note, why swallow the ArgumentError? This will make parsing errors much more difficult to find, I know that they should not happen in production but still when testing they happened to me and was tedious to bind the cause to the specific line.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The situation is described here. Basically, we don't want end users to be able to crash our server at their will, so we need to rescue and swallow. We could keep the nil return but add some logging in between or even append an error to the attribute, but I think the current version is enough for such an edge case...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I understand now. I did not get that the user would provoke a crash when reading your comment before. Indeed raising an error is not what we want.

@deivid-rodriguez
Copy link
Contributor

@tramuntanal Could you merge latest master to bring this PR up to date?

@ghost ghost added the status: WIP label Aug 27, 2018
@mrcasals
Copy link
Contributor

I've rescheduled a CI job, it failed due to a random error

@tramuntanal
Copy link
Contributor Author

Thank you @mrcasals , this PR is now green!

@mrcasals
Copy link
Contributor

@deivid-rodriguez do you mind reviewing the code? 😄

Copy link
Contributor

@deivid-rodriguez deivid-rodriguez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be the same patch as before, only rebased. Right @tramuntanal? It looks good to me from a quick look, except for the change log.

CHANGELOG.md Outdated
- **decidim-initiatives**: Only show initiative types fomr the current tenant [\#3887](https://github.com/decidim/decidim/pull/3887)
- **decidim-core**: Allows users with admin access to preview unpublished components [\#4016](https://github.com/decidim/decidim/pull/4016)
- **decidim-proposals**: Rename "votes" column to "supports" when exporting proposals [\#4018](https://github.com/decidim/decidim/pull/4018)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we've fixed this many things in this PR. Merge problem?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be a problem with a rebase after we released the 0.14.0 version.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just fixed the CHANGELOG @mrcasals

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@deivid-rodriguez , yes, I only merged master to solve conflicts.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok! I'll merge this as soon as CI passes!

@ghost ghost added the status: WIP label Sep 13, 2018
@deivid-rodriguez
Copy link
Contributor

Going in finally!

@deivid-rodriguez deivid-rodriguez merged commit 7c44534 into master Sep 13, 2018
@deivid-rodriguez deivid-rodriguez deleted the fix/3574-date_field_doesnt_work_when_date_is_entered_manually branch September 13, 2018 23:47
@tramuntanal
Copy link
Contributor Author

👏 🎉 🎉 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants