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 name and id for checkbox/radio #251

Closed
wants to merge 7 commits into from

Conversation

bikerduweb
Copy link
Contributor

When using bootstrap_form_tag, "name", "id" and "for" properties for check_box and radio_button are not properly generated, because there is no objet_name to refer to.

This patch fix that to restore proper naming of checkboxes and radio buttons.

@mattbrictson
Copy link
Contributor

Thanks for the PR! It has been a while since I've touched this code, so I will need your help to be able to fully review and merge this.

Could you explain in more detail the bug you are fixing, and what changes were necessary?

Also, I'll need you to fix the test failure and add a CHANGELOG entry before this can be merged. Thanks!

@bikerduweb
Copy link
Contributor Author

bikerduweb commented Dec 5, 2016

In current version of bootstrap_form_tag, name, id and for properties are not properly generated just because there is no object name to refer to.
Exemple from your test:
<input id="_misc" name="[misc]" type="checkbox" value="1" />
=> Here we see that something is missing in the id, ie should be objectname_misc and not just "_misc", and same for name which should be objectname[misc] instead of [misc]... I say should because it would be like that if we had an object... But when we don't have any object, then the proper way to generate all this in rails is id="misc" and name="misc" ...
So my patch make sure that name and id are generated the rails way

The second patch allow to be able to pass "for" parameter to the label in a control group. In the current version, there is no way to specify a specific for parameter ... It also fix the way for parameter is generated in a control group when a specific id have been given.

I'm not fluent in english so it's hard to turn all this in proper english. I've fixed the tests

@mattbrictson
Copy link
Contributor

Thanks for the explanation! I'll review this week. In the meantime, can you add an entry to the CHANGELOG file as part of this PR?

@bikerduweb
Copy link
Contributor Author

Matt, I've added the changelog, but I'm not sure if it's good english ;-) I also tried to reference a few tickets that seems linked to the fix in this PR. So it should Fix #266, Fix #193, Fix #241 and Fix #213

@dan-klasson
Copy link

I've tried this PR and I'm still getting name="[foo]"

@bikerduweb
Copy link
Contributor Author

Sorry to hear that Dan. I use my patch in production, and it work properly so I'm not sure why it isn't working for you. There is also a test that validate the name format. Maybe you could try to debug your code or provide an example of what you do exactly ?

@bikerduweb
Copy link
Contributor Author

bikerduweb commented Dec 8, 2016

If you need, here is a live example:

    <%= bootstrap_form_tag url:'/', html: { role: 'form' } do |form| %>
      <%= form.text_field :email_id, placeholder: 'Mail ID' %>
    <% end %>

it converts to

<form role="form" action="/" accept-charset="UTF-8" method="post">
<input name="utf8" type="hidden" value="✓">
<input type="hidden" name="authenticity_token" value="eNOQVLssnDo+DWNMvsFFjx+Nuos9qXUPK857Pr2LXHmAfUqlDPPAM7R5NobLCLyJAyZCloBYaXcOf9E0IIVqBQ==">
<div class="form-group">
  <label class="sr-only control-label" for="email_id">Email</label>
  <div class="input-group">
    <input placeholder="Mail ID" class="form-control" name="email_id" id="email_id" type="text"> 
  </div>
</div>
</form>

@dan-klasson
Copy link

dan-klasson commented Dec 8, 2016

@veilleperso

This is what I did:

  1. cloned the repo

  2. fetched the PR: git fetch origin pull/251/muweb

  3. git checkout muweb

  4. added to Gemfile: gem 'bootstrap_form', path: '~/rails-bootstrap-forms'

  5. Added this:

    <%= bootstrap_form_tag url: listing_path(@Listing) do |f| %>
    <%= f.select :foo, options_for_select([1, 2]) %>
    <% end %>

But got this output:

<form role="form" action="/listings/1" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="✓">
  <div class="form-group">
    <label class="control-label" for="foo">Foo</label>
    <select class="form-control" name="[foo]" id="_foo">
      <option value="1">1</option>
      <option value="2">2</option>
    </select>
  </div>
</form>

@bikerduweb
Copy link
Contributor Author

I see: i just fixed the generation of name and id for checkbox/radio (as stated in the tile), and that extend to any input field as they work the same. But I didn't take care of select tag which use a different code. I will look into it.

@dan-klasson
Copy link

Also, using the documented way for radio buttons, makes the html come out like this:

<input id="rent_sale_{:value=>&quot;foo&quot;}" name="rent_sale" type="radio" value="{:value=>&quot;foo&quot;}">

@bikerduweb
Copy link
Contributor Author

bikerduweb commented Dec 8, 2016

I think that for radio_button the proper way to do it is not use :value => "foo" , but to pass the value as you would with bootstrap_form ... So your bug seems to be much more related to the way you build your code, as you seem to have copied the exemple of a text_field to a radio_button, but they are not built the same way ...
For the proper way to use radio_button, please use the same way as you do in a classic boostrap_form / rails form

@dan-klasson
Copy link

@veilleperso Select works now as well. Thank you

@dan-klasson
Copy link

Same problem with hidden fields though

@bikerduweb
Copy link
Contributor Author

hidden_field are not processed by rails-bootstrap-form, I can't find any specific code related to hidden field, so I guess the problem is that hidden field are delegated to rails without specific processing by rails-bootstrap-form like for all other fields ...
I don't have to time to fix that because it's not related to my patch. Once hidden_field are handled by rails-bootstrap-form, then I think that the current patch will work like for any other input field.

But feel free to address this specific issue if you need it for your own use.

@mattbrictson
Copy link
Contributor

@veilleperso @dan-klasson Thanks for all your help on this! Would you say this is ready for merge?

Also, if we are going to leave hidden_field for a future PR, could you please create a GitHub issue in the meantime that describes the problem? That way we won't lose track of it.

@bikerduweb
Copy link
Contributor Author

@mattbrictson no need for a PR, i had a quick look this morning and i guess this last patch should fix the hidden_field tag... I also added a test for it.

I guess now everything is ready to merge.

When using bootstrap_form_tag, "name" and "for" properties for check_box and radio_button are not properly generated, because there is no objet to refer to
This patch fix that to restore proper naming.
When generating a label in a form_group, we preserve the +for+ information if this information exist
* +for+ parameter must be properly according to specified id, when an id is provided
* +for+ parameter must be used when provided in label hash params
* Follow the rails naming rules for id and name when using bootstrap_form_tag and select tag
* Allow the use of label: "My String" in form_group, to mimic what is allowed in other bootstrap tags (ie label can be either a string or a hash)
Use the proper rails naming convention for id/name when using bootstrap_form_tag
Use the new assert_equivalent_xml instead of assert_equal
@dan-klasson
Copy link

Still having the problem with the hidden field. But can leave for later.

@bikerduweb
Copy link
Contributor Author

Sorry Dan, but i don't see any problem on hidden field

    <%= bootstrap_form_tag url: '/' do |form| %>
      <%= form.hidden_field :email, value: 'hello@yahoo.com' %>
    <% end %>

gives me

<form role="form" action="/" accept-charset="UTF-8" method="post">
<input name="utf8" type="hidden" value="✓">
<input type="hidden" name="authenticity_token" value="4IAdC35B25rLlhGIb06N2ccqU9FNPnFfHyTirSu3wuDs53eNzGk7EIiqcIqt2cMJ8ftyYJixYV0BEFuW4uMUFQ==">
<input value="hello@yahoo.com" id="email" name="email" type="hidden">
</form>

@dan-klasson
Copy link

@veilleperso hidden fields with form_for is not working for me. Neither is multiselect.

@bikerduweb
Copy link
Contributor Author

Sorry to hear that @dan-klasson but it's outside of the scope of this patch: I only patched a few things for bootstrap_form_for only, mainly for my own needs at first...
You now speak of problems with form_for, feel free to investigate on that subject if there are bugs there, and maybe patch them, as I didn't saw any bug on using form_for + hidden_field ... For multiselect, I don't use them, sorry.

BTW "not working" is a pretty vague concept, so it's hard to help you ;-) It would probably help if instead you show us some code that "don't work" ?

@dan-klasson
Copy link

@veilleperso No worries. Not sure how to use hidden field with form_for and bootstrap form. f.hidden_field throws a nilclass exception. hidden_field produces bar[] instead of foo[bar]. Multiselect has the same issue as select before you fixed it.

I'll review your code changes and submit my own PR.

@bikerduweb
Copy link
Contributor Author

bikerduweb commented Dec 14, 2016

@dan-klasson Here is an exemple of use from code in production, where "Invoice" is an activerecord object with a field customer_id

    <%= form_for(Invoice.first, url: '/') do |form| %>
      <%= form.hidden_field :customer_id, id: 'bill_customer_id'  %>
    <% end %>

The resulting html is:

<form class="edit_invoice" id="edit_invoice_6" action="/" accept-charset="UTF-8" method="post">
<input name="utf8" type="hidden" value="&#x2713;" />
<input type="hidden" name="_method" value="patch" /><input type="hidden" name="authenticity_token" value="4X1M4x18fTj17LUSr+j2ZGULbIDCnTGw3MUJ3e3p9HJRSyHt7WsSvVGdEOLAY+TPI9BWVGm1CgqE6XS7ZMtXPQ==" />
<input id="bill_customer_id" type="hidden" value="5" name="invoice[customer_id]" />
</form>

As you can see the hidden field is properly generated, using the id I manually specified, and properly building the name field

So I guess that the problem is much more in the way you write your code, than in the gem or my patch. Again, my patch didn't concern form_for, only bootstrap_form_for.

@mattbrictson
Copy link
Contributor

@veilleperso @dan-klasson I lost track of the discussion here. Have all the issues been resolved and is this PR good to merge? Let me know, thanks!

lcreid pushed a commit to lcreid/rails-bootstrap-forms that referenced this pull request Jun 4, 2018
If provided, use option `id` to specify `for` attribute on label
for (text, email...) fields, checkbox, radio and select.
Solves bootstrap-ruby#342 issue and supercede bootstrap-ruby#221 and bootstrap-ruby#343 pull requests.

Partially solves bootstrap-ruby#251 (only the part regarding the id).
@lcreid
Copy link
Contributor

lcreid commented Oct 28, 2018

Thank you for your interest in improving bootstrap_form. bootstrap_form has been substantially changed since this PR was submitted. The upgrade to Bootstrap 4 introduced many changes that may have addressed this PR, or made it not applicable. Therefore, we're closing this PR.

If you still believe this PR is needed, please re-open it and update it to be mergeable with the current master. Our apologies for not responding sooner.

@lcreid lcreid closed this Oct 28, 2018
lcreid added a commit that referenced this pull request Mar 1, 2019
* Use option[:id] for label's *for* attribute

If provided, use option `id` to specify `for` attribute on label
for (text, email...) fields, checkbox, radio and select.
Solves #342 issue and supercede #221 and #343 pull requests.

Partially solves #251 (only the part regarding the id).

* Move require mocha/minitest as needed by newer version.

* Fix version of sqlite3 < 1.4 so tests will work.
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.

None yet

4 participants