Add I18n support for :placeholder HTML option is passed to form fields - #16438
Conversation
|
Thank you for the pull request. I like the proposal but I have mixed feelings about it. I know Simple Form already supports this feature and I'm sure we don't want to the default Rails form builder to become Simple Form. @jeremy WDYT? |
|
Great first contribution, @agrobbin ! I'm not sure whether the form helper should be responsible for placeholder i18n either. It's already simple to That said, we do i18n on many other form helpers so it's disappointing that this one spot people rely on doesn't work like the others. Translating symbol values of |
|
The thought here was that a frequent pattern has become to replace @jeremy your suggestion about translating symbol values, if I'm understanding you correctly, feels like it would be very redundant: <%= f.text_field :post, :body, placeholder: :body %>The main issue I've been dealing with in one of the new applications I've been working on is that I've had to continuously include something like this in my forms: <%= f.text_field :post, :body, placeholder: Post.human_attribute_name(:body) %>That just feels gross :) |
|
Is there something I should be doing to update this new feature, or is this request still just something that you are contemplating @rafaelfranca @jeremy? |
|
I'm positive on it, since it matches label i18n. The thing that puzzles me with this and label i18n: Labels and placeholders usually aren't boilerplate text that's identical for every form. I expected to see this try a translation key scoped by the AV template first. Given that label doesn't do this either, fine to match its behavior IMO. But I'd find this behavior frustrating! |
@jeremy I have a PR in the works that adds some other logic to |
|
I've rebased this off of the latest master and added a |
Add I18n support for `:placeholder` HTML option is passed to form fields
|
@agrobbin This looks like the same implementation as label, but that's a bit different - it has both value and content. For a placeholder, a string value needs to remain a literal string. A symbol value could be treated as an i18n key. |
|
@jeremy 👍 I will issue a new PR with that change and regression tests included. |
|
@jeremy this appears to already work as you state with the current implementation of the code. This test appears to pass no problem: def test_text_field_placeholder_with_string_value
with_locale :placeholder do
assert_dom_equal('<input id="post_cost" name="post[cost]" placeholder="How much?" type="text" />', text_field(:post, :cost, placeholder: "How much?"))
end
endAm I missing something? |
|
Ah, figured it out! It was working in tests because of how |
|
Great! Thank you @agrobbin ❤️ |
|
Nice feature. ...But how to omit it? I'm not able to pass an arbitrary value for the placeholder now. I can pass = f.text_field :hostname, placeholder: 'Enter Machine Hostname (e.g. myapp.company.com)'<input id="machine_hostname" name="machine[hostname]" placeholder="Com)" type="text"> |
|
@agrobbin Cool, thanks! |
|
Thank you! This is super helpful! |
Usage is referenced from: rails/rails#16438
Usage is referenced from: rails/rails#16438
Usage is referenced from: rails/rails#16438
Usage is referenced from: rails/rails#16438
The :placeholder option accepts true, so it should be mentioned in the documentation. Ref: rails#16438
This is my first real contribution to the Rails source, so any and all feedback is appreciated!
I looked at #3674 (which appears to have been the implementation of #768), but since that was back in 2012, and there didn't appear to be any actual opposition to this addition, I'm giving this a shot against the latest master.
This adds full I18n support for the
:placeholderHTML5 input/textarea attribute. It tries to mimic thelabelI18n support almost exactly, first checkinghelpers.placeholder, then falling back toobject.class.human_attribute_name, then@method_name.humanize. It only attempts to check for I18n values if you pass the:placeholderoption, as requested in the original PR.Basic usage:
will look up the I18n value in this order:
I thought about extracting the common pieces from this and the
Labeltag, but thought getting feedback on the first pass would make more sense.