[Enhancement] Proper handling of <input type="email" multiple />

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[Enhancement] Proper handling of <input type="email" multiple />

Steve Hoeksema
I'm proposing an enhancement per the contribution guidelines, and looking to find out whether this patch might be accepted.

HTML5 added the "multiple" attribute to <input type="email" /> fields. 
This is useful as it means the user agent can perform email validation and strip whitespace.

However, the actual emails are submitted to the server as a comma-separated string.

Additionally, Rails magic turns something like 

<%= f.email_field :emails, multiple: true %>

into

<input type="email" name="emails[]" multiple />

When submitted, we actually receive this:

params[:emails][0] = "foo,bar"

But we actually want this:

params[:emails] = ["foo", "bar"]

I'm proposing a patch to handle some or all of this unusual (but standards based) behaviour.

When rendering an :email_field with the multiple property:
  • When the ActiveModel attribute is an Array, the <input> value should be joined into a string separated by commas.
  • Alternatively, the value should be absent, and a <datalist> should be added to the output
  • Don't append [] to the <input> name, so the form value remains a string rather than an array of strings
  • Append a hidden form input to indicate the Rails should interpret that form value as a comma-separated string, and turn it into an array

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Enhancement] Proper handling of <input type="email" multiple />

Prem Sichanugrist
I think your proposal looks good. It'd be nice for us to support this feature.

Please submit a PR and ping me on GitHub so I can take a look.

Thank you,
Prem

On Wednesday, August 22, 2018 at 10:13:28 AM UTC+9, Steve Hoeksema wrote:
I'm proposing an enhancement per the contribution guidelines, and looking to find out whether this patch might be accepted.

HTML5 added the <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/email#Allowing_multiple_email_addresses" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fdeveloper.mozilla.org%2Fen-US%2Fdocs%2FWeb%2FHTML%2FElement%2Finput%2Femail%23Allowing_multiple_email_addresses\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHcIqeQT5lZHDK6lIxPK32b2zLtSg&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fdeveloper.mozilla.org%2Fen-US%2Fdocs%2FWeb%2FHTML%2FElement%2Finput%2Femail%23Allowing_multiple_email_addresses\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHcIqeQT5lZHDK6lIxPK32b2zLtSg&#39;;return true;">"multiple" attribute to <input type="email" /> fields. 
This is useful as it means the user agent can perform email validation and strip whitespace.

However, the actual emails are submitted to the server as a comma-separated string.

Additionally, Rails magic turns something like 

<%= f.email_field :emails, multiple: true %>

into

<input type="email" name="emails[]" multiple />

When submitted, we actually receive this:

params[:emails][0] = "foo,bar"

But we actually want this:

params[:emails] = ["foo", "bar"]

I'm proposing a patch to handle some or all of this unusual (but standards based) behaviour.

When rendering an :email_field with the multiple property:
  • When the ActiveModel attribute is an Array, the <input> value should be joined into a string separated by commas.
  • Alternatively, the value should be absent, and a <datalist> should be added to the output
  • Don't append [] to the <input> name, so the form value remains a string rather than an array of strings
  • Append a hidden form input to indicate the Rails should interpret that form value as a comma-separated string, and turn it into an array

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.