1.8 KiB
1.8 KiB
PLAN - Employee Form Error Handling
1. Context & Objective
The user wants to improve the error handling experience when creating or editing an Employee. Currently, when validation fails, a generic "Please correct the errors in the form" toast is displayed, and the user has to manually scroll to find the red fields.
The objective is to:
- Display a more descriptive error toast (e.g. specifying which field is missing).
- Automatically scroll the page to the section/field containing the error so the user can immediately fill it out.
2. Target Files
resources/js/pages/hr/employees/create.tsxresources/js/pages/hr/employees/edit.tsx
3. Task Breakdown
Phase 1: Update Error Toast Message
- In the
onErrorcallback ofrouter.post() / router.put(), retrieve the first validation error from theerrorsobject. - Change
toast.error(t('Please correct the errors in the form'));to display the specific error, e.g.toast.error(Object.values(errors)[0] || t('Please correct the errors in the form'));.
Phase 2: Implement Auto-Scroll to Error
- Add a mechanism inside
onError(possibly wrapped insetTimeoutto ensure DOM updates) to locate the first error element. - Since the fields with errors have a specific class (like
border-red-500or the error text paragraph hastext-red-500), usedocument.querySelector('.text-red-500')or similar. - Call
.scrollIntoView({ behavior: 'smooth', block: 'center' })on the found element. - Optionally call
.focus()on the associated input if applicable.
4. Verification Checklist
- Submit an empty employee creation form.
- Verify the toast displays a specific error message (e.g. "Full Name is required").
- Verify the page smoothly scrolls to the top where the "Full Name" field is.
- Submit an empty employee edit form and verify the same behavior.