Skip to content

feat(Form): automatically focus the first field with an error upon submission failure#6547

Open
rdjanuar wants to merge 5 commits into
nuxt:v4from
rdjanuar:feat/focus-on-error
Open

feat(Form): automatically focus the first field with an error upon submission failure#6547
rdjanuar wants to merge 5 commits into
nuxt:v4from
rdjanuar:feat/focus-on-error

Conversation

@rdjanuar
Copy link
Copy Markdown
Contributor

@rdjanuar rdjanuar commented Jun 3, 2026

🔗 Linked issue

Resolves #4612

❓ Type of change

  • 📖 Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • 👌 Enhancement (improving an existing functionality)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

📚 Description

This PR just a proposal that match with form behavior base on

https://www.w3.org/WAI/WCAG22/Understanding/error-identification.html

in client-validation form when field is error the browser automaticly focus to the first field. currently NuxtUI doesn't do that we need to manage by it self using @error prop. this PR make the Form will automatically focus to the error field

📝 Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

Rizky Djanuar added 2 commits June 3, 2026 21:15
@rdjanuar rdjanuar requested a review from benjamincanac as a code owner June 3, 2026 14:31
@github-actions github-actions Bot added the v4 #4488 label Jun 3, 2026
@rdjanuar rdjanuar changed the title feat(Form): automatically focus the first fieldeld with an error upon submission failure feat(Form): automatically focus the first field with an error upon submission failure Jun 3, 2026
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jun 3, 2026

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 793189d0-f81d-457d-b423-dbf4aa911d02

📥 Commits

Reviewing files that changed from the base of the PR and between 9f0870e and f6e7caa.

📒 Files selected for processing (1)
  • test/components/Form.spec.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • test/components/Form.spec.ts

📝 Walkthrough

Walkthrough

Adds a focusOnError prop (default true) to Form.vue and imports a new scrollToErrorEl(errors) utility that finds the first error by id and focuses its DOM element. On submit validation failure the form calls scrollToErrorEl after nextTick when focusOnError is enabled. Documentation and the example were updated to reflect the behavior and show a toast on error; a test verifies only the first error element is focused.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: automatically focusing the first field with an error upon form submission failure.
Description check ✅ Passed The description relates to the changeset, referencing issue #4612 and explaining the alignment with WCAG error identification guidelines.
Linked Issues check ✅ Passed The PR fully addresses issue #4612 by implementing automatic focus on the first error field via the new focusOnError prop and scrollToErrorEl utility.
Out of Scope Changes check ✅ Passed All changes are in scope: Form.vue prop addition, utility function for scrolling/focusing, documentation updates, example updates, and tests.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (1)
src/runtime/utils/form.ts (1)

118-118: 💤 Low value

Name doesn't match behavior.

scrollToErrorEl only calls focus(); consider focusFirstErrorEl (or similar) to avoid implying explicit scrolling. Browsers may scroll on focus, but the name overstates the intent.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/runtime/utils/form.ts` at line 118, The function name scrollToErrorEl
(exported function scrollToErrorEl) is misleading because it only calls focus()
and does not explicitly scroll; rename it to focusFirstErrorEl (or another name
indicating focus behavior) and update the function declaration, its export, and
all call sites to use the new name, or alternatively implement explicit
scrolling behavior (e.g., element.scrollIntoView) inside scrollToErrorEl if you
want to keep the current name—choose one approach and make the function, its
export, and all references consistent.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/runtime/components/Form.vue`:
- Around line 63-66: Update the JSDoc for the focusOnError prop in Form.vue to
fix the grammar and add an `@defaultValue` tag; change the description to
something like "When true, the form will automatically focus the first form
element with an error" and add "`@defaultValue` false" to document the default
(ensure the runtime default remains controlled via withDefaults() where the prop
is declared). Reference the focusOnError prop to locate the comment and adjust
only the JSDoc text.

In `@src/runtime/utils/form.ts`:
- Around line 118-130: The function scrollToErrorEl currently declares a local
timeOut that never persists (making the clearTimeout branch dead) and
force-asserts error.id; change it to either use a module-scoped timeout variable
(e.g., move timeOut outside the scrollToErrorEl function) if you want
debounce/cancel behavior, or remove the clearTimeout and call setTimeout
directly; additionally, guard the id on the first error (check error &&
error.id) before calling document.getElementById(error.id) to avoid asserting
away the optional id. Ensure references to the local variable name timeOut and
the function scrollToErrorEl are updated accordingly.

---

Nitpick comments:
In `@src/runtime/utils/form.ts`:
- Line 118: The function name scrollToErrorEl (exported function
scrollToErrorEl) is misleading because it only calls focus() and does not
explicitly scroll; rename it to focusFirstErrorEl (or another name indicating
focus behavior) and update the function declaration, its export, and all call
sites to use the new name, or alternatively implement explicit scrolling
behavior (e.g., element.scrollIntoView) inside scrollToErrorEl if you want to
keep the current name—choose one approach and make the function, its export, and
all references consistent.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: c398736b-2c79-4244-846f-433703c2c179

📥 Commits

Reviewing files that changed from the base of the PR and between 0414dd0 and b67b3f6.

📒 Files selected for processing (5)
  • docs/app/components/content/examples/form/FormExampleOnError.vue
  • docs/content/docs/2.components/form.md
  • src/runtime/components/Form.vue
  • src/runtime/utils/form.ts
  • test/components/Form.spec.ts

Comment thread src/runtime/components/Form.vue
Comment thread src/runtime/utils/form.ts
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Jun 3, 2026

npm i https://pkg.pr.new/@nuxt/ui@6547

commit: f6e7caa

Comment thread src/runtime/utils/form.ts Outdated
Comment on lines +125 to +127
timeOut = setTimeout(() => {
el.focus()
}, 0)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why cant we use nextTick() instead?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's because when we submit the Form the field become disabled. i think i can use nextTick outside the try & catch block on function onSubmitWrapper and remove the setTimeout.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

♻️ Duplicate comments (1)
src/runtime/utils/form.ts (1)

120-122: ⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Guard optional id instead of force-asserting it.

FormErrorWithId.id is optional, so error.id! still bypasses the declared contract (Line 121). Please short-circuit when the first error has no id.

Suggested fix
 export function scrollToErrorEl(errors: FormErrorWithId[]) {
   const error = errors[0]
-  if (error) {
-    const el = document.getElementById(error.id!)
-    if (el) {
-      el.focus()
-    }
-  }
+  if (!error?.id) return
+  const el = document.getElementById(error.id)
+  if (el) {
+    el.focus()
+  }
 }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/runtime/utils/form.ts` around lines 120 - 122, The code force-asserts
error.id (error.id!) even though FormErrorWithId.id is optional; update the
logic in the function handling form errors so it short-circuits if the first
error has no id (e.g., check if error.id is falsy before calling
document.getElementById), and only call document.getElementById(error.id) and
proceed to use el when error.id is present; locate the block using the variable
error and the FormErrorWithId type to apply this guard.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Duplicate comments:
In `@src/runtime/utils/form.ts`:
- Around line 120-122: The code force-asserts error.id (error.id!) even though
FormErrorWithId.id is optional; update the logic in the function handling form
errors so it short-circuits if the first error has no id (e.g., check if
error.id is falsy before calling document.getElementById), and only call
document.getElementById(error.id) and proceed to use el when error.id is
present; locate the block using the variable error and the FormErrorWithId type
to apply this guard.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 82ad0129-95fb-463d-a303-3dbc8074095b

📥 Commits

Reviewing files that changed from the base of the PR and between 86509cf and 9f0870e.

📒 Files selected for processing (2)
  • src/runtime/components/Form.vue
  • src/runtime/utils/form.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/runtime/components/Form.vue

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

Labels

v4 #4488

Projects

None yet

Development

Successfully merging this pull request may close these issues.

element.focus() not working in Form component example

2 participants