Skip to content

feat(form): focus the first invalid field on submit - #5264

Open
guillerot wants to merge 2 commits into
release/22.1from
feat/form-focus-first-invalid-on-submit
Open

guillerot wants to merge 2 commits into
release/22.1from
feat/form-focus-first-invalid-on-submit

Conversation

@guillerot

@guillerot guillerot commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

Description

form[luForm] can now move focus to the first invalid field when the form is submitted while invalid, so keyboard and screen reader users are led straight to the error (WCAG 3.3.1). The behavior is opt-in via a new focusInvalidOnSubmit boolean input (default false).


The behavior is DOM-based on purpose: after the submit event, afterNextRender lets the app's own submit handler run first (typically markAllAsTouched) and change detection apply the validity state; then, if the host <form> carries ng-invalid, the innermost .ng-invalid element is resolved and focused. When that element is not natively focusable (custom control hosts like lu-simple-select), the first focusable descendant is focused instead, then scrolled into view.

Working with CSS state classes rather than injecting FormGroupDirective/NgForm keeps it agnostic: it covers reactive forms and ngModel alike, and needs no wiring in consuming apps.

Points to discuss before undrafting:

  • No story yet: I can add a Storybook story demonstrating the behavior if you confirm the approach.

Validated end-to-end in Cleemy Procurement with the same logic as a local directive (supplier creation form: submit via click and via Enter both land focus on the first invalid field).


Contribution

  • Designs are respected and all relevant options are handled
  • Responsive behavior addressed when needed
  • Feature is accessible (keyboard navigation, screen reader support, ARIA tags, etc.)
  • Stories are updated and Storybook controls have descriptions
  • Feature is covered by E2E tests or UI diff
  • npm run build OK
  • npm run lint OK

Functional review

  • UI diff OK
  • Feature and all options are manually tested and comply with design and guidelines.

@guillerot guillerot added this to the 22.0 milestone Aug 21, 2026
@guillerot guillerot added 👥 Accessibility Screen readers, contrasts, readability, keyboard, etc. 🔖✨ Feature New feature (even a very small one) 🤖 Produced by AI Some code was produced by AI. (Autocompleted code doesn't require to be labeled) labels Aug 21, 2026
@github-actions

Copy link
Copy Markdown

🚀 Storybook preview deployed: https://pub-dc6d99acd6874e2aaff6219dd8a13ae2.r2.dev/PR-5264/index.html

@guillerot
guillerot force-pushed the feat/form-focus-first-invalid-on-submit branch from 662420e to 2b8eea4 Compare August 21, 2026 15:13
Comment thread packages/ng/form/form.component.ts Outdated
@guillerot
guillerot marked this pull request as ready for review August 24, 2026 14:59
@guillerot
guillerot requested review from a team as code owners August 24, 2026 14:59
@guillerot
guillerot force-pushed the feat/form-focus-first-invalid-on-submit branch from 2b8eea4 to dccc652 Compare August 24, 2026 15:03
JeSuisCharlie1
JeSuisCharlie1 previously approved these changes Aug 24, 2026
@Supamiu Supamiu modified the milestones: 22.0, 22.1 Aug 24, 2026
@Supamiu

Supamiu commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Pushing it our of 22.0 so we can properly discuss it and find the best approach.

@jeremie-lucca
jeremie-lucca changed the base branch from release/22.0 to release/22.1 September 9, 2026 16:12
@jeremie-lucca
jeremie-lucca dismissed JeSuisCharlie1’s stale review September 9, 2026 16:12

The base branch was changed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

👥 Accessibility Screen readers, contrasts, readability, keyboard, etc. 🔖✨ Feature New feature (even a very small one) 🤖 Produced by AI Some code was produced by AI. (Autocompleted code doesn't require to be labeled)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants