Skip to content

Disable Rails field_with_errors wrapper engine-wide via field_error_proc #339

Description

@jeffhorn-nava

Context

PR #337 added prefix/suffix support to text_field, wrapping inputs in usa-input-group with adjacent prefix/suffix divs. The implementation includes a scoped workaround: when a prefixed/suffixed input is in an error state, Rails' default field_with_errors wrapper is stripped via regex. Without the strip, the wrapper sits between .usa-input-prefix and <input> and breaks the .usa-input-prefix + input CSS adjacency selector that supplies the input's padding-left.

The fix addresses the immediate bug but only for prefix/suffix. Rails injects a <div class="field_with_errors"> wrapper that Strata never wants, and the same bug will reproduce with any future USWDS feature that uses adjacent-sibling selectors (character counters, date pickers, combo boxes).

Proposal

Set Rails' field_error_proc to a pass-through at the engine level, then remove the scoped strip from text_field.

ActionView::Base.field_error_proc = ->(html_tag, _instance) { html_tag }

Why this is safe

Strata already renders its own error markup for every form helper:

  • usa-input--error on the input element
  • usa-form-group--error on the surrounding form group
  • usa-error-message span with the validation message

Rails' field_with_errors wrapper adds nothing on top of that. It's redundant for any consumer of Strata's form builders.

Why this matters now

USWDS uses adjacent-sibling and child selectors in several components:

  • .usa-input-prefix + input (this PR)
  • .usa-input-suffix + .usa-character-count__message (character counters)
  • .usa-date-picker__wrapper > input (date pickers)

Each will hit the same wrapper-breaks-adjacency bug as it gets added. Fixing the wrapper at the engine level preempts the whole class.

Precedent

navapbc/archive-md-famli did this in config/application.rb:112.

Scope of the follow-up PR

  • Add the field_error_proc override in Strata's engine boot config
  • Remove the scoped strip from Strata::FormBuilder#text_field
  • Keep the "blank prefix/suffix as absent" spec (independent of field_error_proc)
  • Adjust or drop the "keeps the input as the immediate sibling of the prefix in error states" spec; the adjacency will hold by default once the wrapper is gone, making that spec a tautology

Open question

Whether to fold an ADR into the follow-up PR. Blast radius (changes rendered HTML for every Strata consumer in error states) and rationale (Strata's error markup makes Rails' redundant) both suggest yes.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requestrubyPull requests that update Ruby code

Projects

Status
Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions