@@ -11,6 +11,13 @@ def initialize(input:)
1111 @input . add_label_classes ( "FormControl-label" )
1212 @input . add_input_classes ( "FormControl-checkbox" )
1313
14+ # Generate custom ID that preserves brackets from the name
15+ unless @input . input_arguments [ :id ] . present?
16+ generate_custom_id
17+ # Update the label's for attribute to match the new ID
18+ @input . label_arguments [ :for ] = @input . input_arguments [ :id ]
19+ end
20+
1421 return unless @input . scheme == :array
1522
1623 @input . input_arguments [ :multiple ] = true
@@ -32,6 +39,27 @@ def nested_form_arguments
3239
3340 private
3441
42+ def generate_custom_id
43+ # Generate an ID from the name that preserves special characters like brackets
44+ # For array scheme: name + "_" + value (e.g., "permissions[3]_foo")
45+ # For boolean scheme: just the name (e.g., "long_o")
46+ base_name = @input . name . to_s
47+
48+ # For array scheme, Rails appends [] to the name, so we remove it for ID generation
49+ # but only the trailing [] that Rails adds, not brackets that are part of the original name
50+ # Regex /\[\]$/ matches literal "[]" at the end of the string
51+ base_name = base_name . sub ( /\[ \] $/ , "" )
52+
53+ # For array scheme, append the value to make IDs unique
54+ # For boolean scheme, just use the base name
55+ # Note: Rails automatically escapes HTML attributes, so special characters are safe
56+ if @input . scheme == :array && @input . value . present?
57+ @input . input_arguments [ :id ] = "#{ base_name } _#{ @input . value } "
58+ else
59+ @input . input_arguments [ :id ] = base_name
60+ end
61+ end
62+
3563 def checked_value
3664 @input . value || "1"
3765 end
0 commit comments