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

field_error_proc wrap form select optgroup and separator option #31088

Merged
merged 1 commit into from Nov 9, 2017

Conversation

Projects
None yet
4 participants
@neumayr
Copy link
Contributor

neumayr commented Nov 8, 2017

Background

The :field_error_proc is responsible for decorating input tags that refer to attributes with errors. This default build-in rails feature wrap invalid form elements with additional markup: <div class="field_with_errors">[…]</div>.

Bug on form select with grouped options

The optgroup of a select collection is wraped by the field_error_proc

<%
  grouped_options = [
    ["A", [["A1"], ["A2"]]],
    ["B", [["B1"], ["B2"]]],
  ]
%>

<%= f.select("category", grouped_options, prompt: true) %>
<div class="field_with_errors">  <!---->
  <select name="post[category]" id="post_category">
    <option value="">Please select</option>
    <div class="field_with_errors">  <!-- ⚡️ -->
      <optgroup label="A">
        <option value="A1">A1</option>
        <option value="A2">A2</option>
      </optgroup>
    </div>

    <div class="field_with_errors">  <!-- ⚡️ -->
      <optgroup label="B">
        <option value="B1">B1</option>
        <option value="B2">B2</option>
      </optgroup>
    </div>
  </select>
</div>

Bug on time_zone_select with priority_zones

For a time_zone_select with priority_zones, the divider is processed by field_with_errors

<%= f.time_zone_select :time_zone, /Hawaii/, include_blank: true %>
<div class="field_with_errors"> <!---->
  <select id="post_time_zone" name="post[time_zone]">
    <option value=""></option>
    <option value="Hawaii">(GMT-10:00) Hawaii</option>
    <div class="field_with_errors">  <!-- ⚡️ -->
      <option value="" disabled="disabled">-------------</option>
    </div>
    <option value="American Samoa">(GMT-11:00) American Samoa</option>
    […]
    <option value="Tokelau Is.">(GMT+13:00) Tokelau Is.</option>
  </select>
</div>

Notice

⚠️ Chrome Web Inspector didn't render div elements inside a select, guess because it's invalid html. It is only visible in the raw source code.

System configuration

Ruby v2.4.2
Rails v5.1.4


  • Fix for field_error_proc wraps form select optgroup
  • Fix for field_error_proc wraps form select divider option
  • Add tests for uncovered elements with errors

Test coverage

  • test_select_grouped_options_with_errors
  • test_time_zone_select_with_priority_zones_and_errors

Extend test coverage

  • test_collection_select_with_errors
  • test_label_with_errors
  • test_check_box_with_errors
  • test_check_boxes_with_errors
  • test_radio_button_with_errors
  • test_radio_buttons_with_errors
  • test_collection_check_boxes_with_errors
  • test_collection_radio_buttons_with_errors
@rails-bot

This comment has been minimized.

Copy link

rails-bot commented Nov 8, 2017

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @pixeltrix (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review.

Please see the contribution instructions for more information.

@rafaelfranca

This comment has been minimized.

Copy link
Member

rafaelfranca commented Nov 9, 2017

I don't know if it's a good idea to start adding if-conditions like someone did with tag_generate_errors?. To guard error_wrapping for optgroup and option value="" disabled tag-elements.

I think we can start with that. There is no reason to us to keep this bug happening just because we want to write a perfect code.

@neumayr neumayr changed the title add check_box/radio_buttons/collection_* test to ActiveModelHelperTest field_error_proc wrap form select optgroup and separator option Nov 9, 2017

Fix field_error_proc wrap form select optgroup and divider option tag
### Summary

The [`:field_error_proc`](https://github.com/rails/rails/blob/master/actionview/lib/action_view/base.rb#L145) is responsible for decorating input tags that refer to attributes with errors. This default build-in rails feature wrap invalid form elements with additional markup: `<div class="field_with_errors">[…]</div>`.

* Fix for `field_error_proc` wraps form select `optgroup`
* Fix for `field_error_proc` wraps form select divider `option`
* Add tests for uncovered elements with errors

[Fixes #31088]

#### Test coverage
* `test_select_grouped_options_with_errors`
* `test_time_zone_select_with_priority_zones_and_errors`

#### Extend test coverage
* `test_collection_select_with_errors`
* `test_label_with_errors`
* `test_check_box_with_errors`
* `test_check_boxes_with_errors`
* `test_radio_button_with_errors`
* `test_radio_buttons_with_errors`
* `test_collection_check_boxes_with_errors`
* `test_collection_radio_buttons_with_errors`

@neumayr neumayr force-pushed the neumayr:am-helper-missing-field_with_errors branch to ead4776 Nov 9, 2017

@rafaelfranca rafaelfranca merged commit dac7c88 into rails:master Nov 9, 2017

1 of 2 checks passed

continuous-integration/travis-ci/pr The Travis CI build failed
Details
codeclimate All good!
Details

@neumayr neumayr deleted the neumayr:am-helper-missing-field_with_errors branch Nov 9, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.