Skip to content

Fixing id generation on radio buttons. fixes #194#195

Merged
josevalim merged 1 commit into
phoenixframework:masterfrom
GBH:master
Jan 22, 2018
Merged

Fixing id generation on radio buttons. fixes #194#195
josevalim merged 1 commit into
phoenixframework:masterfrom
GBH:master

Conversation

@GBH
Copy link
Copy Markdown
Contributor

@GBH GBH commented Jan 22, 2018

Made a input_id/3 just so it's easier to generate for attribute on labels. Maybe it makes sense to expand label to allow value passing. Not sure. One thing at the time.

Comment thread lib/phoenix_html/form.ex
do: "#{id}_#{field}"
def input_id(name, field) when is_atom(name),
do: "#{name}_#{field}"
def input_id(name, field, value) do
Copy link
Copy Markdown
Member

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 need to make this function public. What if we call it radio_button_id and keep it private for now?

Copy link
Copy Markdown
Contributor Author

@GBH GBH Jan 22, 2018

Choose a reason for hiding this comment

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

This is basically so I can do label(name, field, for: input_id(name, field, value)). Otherwise I gotta re-implement that again. Also this method is useful for checkbox collection (when you have name: "search[keys][] for example) and need unique ids and for attributes just like for radio buttons.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I see, agreed. If that's the case, we need to add documentation to it, as it is effectively a separate function than input_id/2.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Will do

Comment thread lib/phoenix_html/form.ex Outdated
def input_id(name, field) when is_atom(name),
do: "#{name}_#{field}"
def input_id(name, field, value) do
value_id = value |> String.downcase |> String.replace(~r/\W/, "_")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why do we need to downcase?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We should probably set the Unicode flag in the regex: ~r/\W/u.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Downcasing is just to make it look consistent. "search_key_foo" looks better than "search_key_FoO". Gotcha on the unicode thing (coming from ruby I didn't know it's a thing)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's remove downcase then. It is not necessary. If people want it downcase, then they can pass it explicitly using the input_id/3 function. :D

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fair enough. Will update PR for tomorrow.

@josevalim
Copy link
Copy Markdown
Member

I have added some comments, thank you @GBH!

@GBH
Copy link
Copy Markdown
Contributor Author

GBH commented Jan 22, 2018

@josevalim PR is amended

@josevalim josevalim merged commit 658a0cc into phoenixframework:master Jan 22, 2018
@josevalim
Copy link
Copy Markdown
Member

❤️ 💚 💙 💛 💜

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants