Fix label for not matching input id when collection value is nil#1867
Open
55728 wants to merge 1 commit intoheartcombo:mainfrom
Open
Fix label for not matching input id when collection value is nil#186755728 wants to merge 1 commit intoheartcombo:mainfrom
for not matching input id when collection value is nil#186755728 wants to merge 1 commit intoheartcombo:mainfrom
Conversation
When a collection contains a nil value (e.g. `[['Undefined', nil]]`),
the label's `for` attribute gets a trailing underscore
(`for="model_method_"`) while the input's `id` does not
(`id="model_method"`), making the label non-functional.
The root cause is in Rails' `sanitize_attribute_name`, which always
concatenates an underscore separator before the sanitized value
(`"#{sanitized_method_name}_#{sanitized_value(value)}"`), even when
`sanitized_value(nil)` returns an empty string.
Override `sanitize_attribute_name` in `CollectionExtensions` to skip
the trailing underscore when the sanitized value is empty, aligning
the label's `for` with the input's `id`.
Fixes heartcombo#1840
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.
Fixes #1840.
Problem
When a collection contains a
nilvalue, the generated label'sforattribute does not match the corresponding input'sid:produces:
Clicking the "Undefined" label does not select its radio button because the
for/idvalues differ by a single trailing underscore.Cause
Rails'
sanitize_attribute_nameunconditionally interpolates an underscore separator:When
valueisnil,sanitized_value(nil)returns"", producing"my_method_"(trailing underscore). This value is passed to the label builder as the method name, which generatesfor="model_my_method_".Meanwhile, the radio button's
idis generated byadd_default_name_and_id_for_value, which has a separate nil-check that skips the value suffix entirely, producingid="model_my_method"(no trailing underscore).Fix
Override
sanitize_attribute_nameinSimpleForm::Tags::CollectionExtensionsto skip the underscore when the sanitized value is empty:This aligns the label's
forwith the input'sidfor bothcollection_radio_buttonsandcollection_check_boxes, in both inline and nested boolean styles.