Form Validation
Validation combines correctness and UX: when errors appear, how they are announced, and how client rules match the server.
When to Offer This Workflow
Trigger conditions:
- - Long forms, wizards, or multi-step checkouts
- Accessibility audits flagging unclear errors
- Mismatch between client-side “valid” and API rejection
Initial offer:
Use six stages: (1) model & schema, (2) rule layers, (3) UX timing, (4) accessibility, (5) async & server parity, (6) testing). Confirm framework (React Hook Form, Formik, native, etc.).
Stage 1: Model & Schema
Goal: Single schema (Zod, Yup, JSON Schema) as source of truth; share with backend when feasible.
Stage 2: Rule Layers
Goal: Separate required/format rules from cross-field rules (e.g., date range); isolate async checks (username available) from fast inline validation.
Stage 3: UX Timing
Goal: Choose onBlur vs onSubmit per field; avoid shouting on every keystroke unless real-time feedback is a product requirement.
Stage 4: Accessibility
Goal: Associate errors with fields via aria-describedby; move focus to first error on submit; use live regions judiciously.
Stage 5: Async & Server Parity
Goal: Map API validation errors to fields; handle race conditions on slow networks; idempotent submit with dedupe if needed.
Stage 6: Testing
Goal: Unit-test schema; e2e critical paths including server error mapping.
Final Review Checklist
- - [ ] Schema aligned or shared with server
- [ ] Validation timing and copy defined
- [ ] Accessible error patterns
- [ ] API errors mapped to UI
- [ ] Tests for schema and flows
Tips for Effective Guidance
- - Server rules always win—document intentional client shortcuts.
- Pair with ux-writing for error microcopy.
Handling Deviations
- - Wizards: validate per step and on final submit; persist drafts securely.
表单验证
验证结合了正确性与用户体验:错误出现时如何呈现、如何播报,以及客户端规则如何与服务器端匹配。
何时提供此工作流程
触发条件:
- - 长表单、向导或多步骤结账流程
- 无障碍审核指出错误提示不清晰
- 客户端显示有效但API拒绝提交
初始方案:
使用六个阶段:(1) 模型与模式、(2) 规则层级、(3) 用户体验时机、(4) 无障碍、(5) 异步与服务器一致性、(6) 测试)。确认框架(React Hook Form、Formik、原生等)。
阶段1:模型与模式
目标: 以单一模式(Zod、Yup、JSON Schema)作为唯一数据源;在可行时与后端共享。
阶段2:规则层级
目标: 将必填/格式规则与跨字段规则(如日期范围)分离;将异步检查(如用户名可用性)与快速内联验证隔离。
阶段3:用户体验时机
目标: 按字段选择onBlur或onSubmit触发验证;除非实时反馈是产品需求,否则避免每次按键都触发验证提示。
阶段4:无障碍
目标: 通过aria-describedby将错误与字段关联;提交时将焦点移至首个错误字段;审慎使用实时区域。
阶段5:异步与服务器一致性
目标: 将API验证错误映射到对应字段;处理慢速网络下的竞态条件;必要时实现带去重的幂等提交。
阶段6:测试
目标: 对模式进行单元测试;对关键路径(包括服务器错误映射)进行端到端测试。
最终审查清单
- - [ ] 模式与服务器对齐或共享
- [ ] 验证时机与文案已定义
- [ ] 无障碍错误模式
- [ ] API错误已映射到UI
- [ ] 模式与流程的测试
有效指导技巧
- - 服务器规则始终优先——记录有意的客户端简化方案。
- 与用户体验文案配合编写错误微文案。
偏差处理
- - 向导:在每一步和最终提交时进行验证;安全保存草稿。