{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "validate-on-blur-then-change",
  "title": "Validate on Blur, then on Change",
  "description": "The \"reward early, punish late\" strategy: a field stays quiet while typing, validates when you leave it, and once it has shown an error it re-validates on every keystroke so the message clears the moment it is fixed. Use this when live validation feels nagging but a stale error feels broken.",
  "registryDependencies": [
    "field",
    "input",
    "@tealmac/form-store"
  ],
  "files": [
    {
      "path": "registry/patterns/validate-on-blur-then-change/validate-on-blur-then-change.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\n\nimport {\n  Field,\n  FieldDescription,\n  FieldError,\n  FieldLabel,\n} from \"@/components/ui/field\"\nimport { useFormStore } from \"@/components/ui/form-store\"\nimport { Input } from \"@/components/ui/input\"\n\nconst rules = {\n  email: (v: string) =>\n    /^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$/.test(v)\n      ? []\n      : [\"Enter an email like name@example.com.\"],\n  code: (v: string) => (/^\\d{6}$/.test(v) ? [] : [\"The code has 6 digits.\"]),\n}\n\nexport function ValidateOnBlurThenChange() {\n  // The form store validates on blur and on change; a field shows messages only after it has been\n  // blurred once (touched), and from then on every change updates them. That is the whole strategy.\n  const form = useFormStore({\n    defaultValues: { email: \"\", code: \"\" },\n    validators: {\n      change: (v) => ({\n        email: rules.email(v.email),\n        code: rules.code(v.code),\n      }),\n    },\n  })\n\n  return (\n    <form\n      noValidate\n      className=\"flex w-full max-w-sm flex-col gap-4\"\n      onSubmit={form.handleSubmit}\n    >\n      {([\"email\", \"code\"] as const).map((name) => (\n        <form.Field<string> key={name} name={name}>\n          {(field) => (\n            <Field data-invalid={field.invalid || undefined}>\n              <FieldLabel htmlFor={`vbc-${name}`}>\n                {name === \"email\" ? \"Email\" : \"Verification code\"}\n              </FieldLabel>\n              <Input\n                id={`vbc-${name}`}\n                inputMode={name === \"code\" ? \"numeric\" : undefined}\n                {...field.inputProps}\n              />\n              {field.invalid ? null : (\n                <FieldDescription>\n                  {field.touched && field.allErrors.length === 0\n                    ? \"Looks good.\"\n                    : \"Checked when you leave the field.\"}\n                </FieldDescription>\n              )}\n              <FieldError errors={field.errors} />\n            </Field>\n          )}\n        </form.Field>\n      ))}\n    </form>\n  )\n}\n",
      "type": "registry:block"
    }
  ],
  "type": "registry:block"
}