-
Sean Doyle authored
As a follow-up to [rails/rails#40479][], ensure that empty Hash and Array arguments are treated as `nil`. For example, when conditionally rendering an [aria-describedby][] attribute to associate an input with a related validation error, treat an empty Array as `nil`, an omit the attribute entirely: ```html+erb <% post = Post.new %> <%= form_with model: post do |form| %> <%= form.text_field :title, aria: { describedby: { post_title_error: post.errors[:title].any? } } %> <%= tag.span(post.errors[:title].to_sentence, id: :post_title_error) if post.errors[:title].any? %> <% end %> ``` In this example, when there are no errors, the desired outcome is for the `<input type="text" name="post[title]">` element to _omit_ the `[aria-describedby="post_title_error"]` attribute, and to only include it when there are errors on the `title` attribute. Without this change, the desired outcome can be achieved with a combination of a `#token_list` and `#presence` call: ```diff <% post = Post.new %> <%= form_with model: post do |form| %> - <%= form.text_field :title, aria: { describedby: {post_title_error: post.errors[:title].any?} } + <%= form.text_field :title, aria: { describedby: token_list(post_title_error: post.errors[:title].any?).presence } %> <%= tag.span(post.errors[:title].to_sentence, id: :post_title_error) if post.errors[:title].any? %> <% end %> ``` [rails/rails#40479]: https://github.com/rails/rails/pull/40479 [aria-describedby]: https://www.w3.org/WAI/WCAG21/Techniques/aria/ARIA21#example-2-identifying-errors-in-data-format
Sean Doyle authoredAs a follow-up to [rails/rails#40479][], ensure that empty Hash and Array arguments are treated as `nil`. For example, when conditionally rendering an [aria-describedby][] attribute to associate an input with a related validation error, treat an empty Array as `nil`, an omit the attribute entirely: ```html+erb <% post = Post.new %> <%= form_with model: post do |form| %> <%= form.text_field :title, aria: { describedby: { post_title_error: post.errors[:title].any? } } %> <%= tag.span(post.errors[:title].to_sentence, id: :post_title_error) if post.errors[:title].any? %> <% end %> ``` In this example, when there are no errors, the desired outcome is for the `<input type="text" name="post[title]">` element to _omit_ the `[aria-describedby="post_title_error"]` attribute, and to only include it when there are errors on the `title` attribute. Without this change, the desired outcome can be achieved with a combination of a `#token_list` and `#presence` call: ```diff <% post = Post.new %> <%= form_with model: post do |form| %> - <%= form.text_field :title, aria: { describedby: {post_title_error: post.errors[:title].any?} } + <%= form.text_field :title, aria: { describedby: token_list(post_title_error: post.errors[:title].any?).presence } %> <%= tag.span(post.errors[:title].to_sentence, id: :post_title_error) if post.errors[:title].any? %> <% end %> ``` [rails/rails#40479]: https://github.com/rails/rails/pull/40479 [aria-describedby]: https://www.w3.org/WAI/WCAG21/Techniques/aria/ARIA21#example-2-identifying-errors-in-data-format
Loading