Add label_for option to wrappers#1868
Open
55728 wants to merge 1 commit intoheartcombo:mainfrom
Open
Conversation
When `label_for: true` is set in a wrapper definition, the wrapper
automatically adds the `for` attribute pointing to the corresponding
input element. The value is resolved from `input_html: { id: }` if
present, otherwise from `ActionView::Helpers::FormHelper#field_id`.
This allows label elements used as wrappers to be properly associated
with their input fields, improving accessibility for custom wrapper
layouts. Works correctly with nested forms as `field_id` sanitizes
the full `object_name` path consistently with Rails' input ID
generation.
Closes heartcombo#1800
673b7fc to
e452277
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1800
When building custom wrappers with
tag: :label, there was no way to automatically add theforattribute pointing to the corresponding input element.This PR adds a
label_foroption to the wrapper DSL. When set totrue, the wrapper resolves the input'sidand adds it as theforattribute on the wrapping element.Usage
This produces:
How it works
label_for: trueis opt-in — existing wrappers are not affectedinput_html: { id: "custom_id" }is provided, that value is usedfield_id(Rails 7.0+) generates the standard input IDforattribute set viahtml: { for: "..." }takes precedence (||=)simple_fields_for) —field_idsanitizes the fullobject_namepath consistently with Rails' input ID generation