Skip to main content
v7

useForm

Form elements are useful when you need to accept user data entries. This library provides a useForm hook that integrates with Zod to create forms as fast as possible.

Package@g4rcez/componentsSectionForm Controls

Examples

Dynamic Form with Zod Validation

Demonstrates a comprehensive form using `useForm` hook with Zod schema validation, including various input types, dynamic fields, and submission handling.

Form Debug State

Displays the current state of the form object, useful for debugging and understanding form data flow.

Debug - User:
{
  "state": {},
  "fields": [
    {
      "name": "agree",
      "kind": "boolean",
      "options": [],
      "required": true,
      "constraints": {},
      "children": [],
      "schema": {
        "def": {
          "type": "literal",
          "values": [
            true
          ]
        },
        "type": "literal",
        "values": {}
      },
      "nullable": false,
      "hasDefault": false,
      "meta": {},
      "title": "Agree",
      "component": "checkbox"
    },
    {
      "name": "number",
      "kind": "number",
      "options": [],
      "required": true,
      "constraints": {
        "type": "number"
      },
      "children": [],
      "schema": {
        "def": {
          "type": "number",
          "checks": []
        },
        "type": "number",
        "isFinite": true
      },
      "nullable": false,
      "hasDefault": false,
      "meta": {},
      "title": "Number",
      "component": "input"
    },
    {
      "name": "boolean",
      "kind": "boolean",
      "options": [],
      "required": true,
      "constraints": {},
      "children": [],
      "schema": {
        "def": {
          "type": "literal",
          "values": [
            true
          ]
        },
        "type": "literal",
        "values": {}
      },
      "nullable": false,
      "hasDefault": false,
      "meta": {},
      "title": "Boolean",
      "component": "checkbox"
    },
    {
      "name": "languages",
      "kind": "multi",
      "options": [],
      "required": true,
      "constraints": {},
      "children": [],
      "schema": {
        "def": {
          "type": "array",
          "element": {
            "def": {
              "type": "string"
            },
            "type": "string"
          }
        },
        "type": "array",
        "element": {
          "def": {
            "type": "string"
          },
          "type": "string"
        }
      },
      "nullable": false,
      "hasDefault": false,
      "defaultValue": [],
      "meta": {},
      "title": "Languages",
      "component": "multiselect"
    },
    {
      "name": "date",
      "kind": "datetime",
      "options": [],
      "required": true,
      "constraints": {},
      "children": [],
      "schema": {
        "def": {
          "type": "string",
          "format": "datetime",
          "check": "string_format",
          "offset": false,
          "local": false,
          "precision": null,
          "pattern": {}
        },
        "type": "string"
      },
      "storage": "iso-datetime",
      "nullable": false,
      "hasDefault": false,
      "meta": {},
      "title": "Date",
      "component": "datepicker"
    },
    {
      "name": "datetime",
      "kind": "datetime",
      "options": [],
      "required": true,
      "constraints": {},
      "children": [],
      "schema": {
        "def": {
          "type": "string",
          "format": "datetime",
          "check": "string_format",
          "offset": false,
          "local": false,
          "precision": null,
          "pattern": {}
        },
        "type": "string"
      },
      "storage": "iso-datetime",
      "nullable": false,
      "hasDefault": false,
      "meta": {},
      "title": "Datetime",
      "component": "datepicker"
    },
    {
      "name": "name",
      "kind": "string",
      "options": [],
      "required": true,
      "constraints": {
        "minLength": 1
      },
      "children": [],
      "schema": {
        "def": {
          "type": "string",
          "checks": [
            {}
          ]
        },
        "type": "string"
      },
      "nullable": false,
      "hasDefault": false,
      "meta": {},
      "title": "Name",
      "component": "input"
    },
    {
      "name": "document",
      "kind": "string",
      "options": [],
      "required": true,
      "constraints": {
        "minLength": 1
      },
      "children": [],
      "schema": {
        "def": {
          "type": "string",
          "checks": [
            {}
          ]
        },
        "type": "string"
      },
      "nullable": false,
      "hasDefault": false,
      "meta": {},
      "title": "Document",
      "component": "input"
    },
    {
      "name": "surname",
      "kind": "string",
      "options": [],
      "required": true,
      "constraints": {
        "minLength": 1
      },
      "children": [],
      "schema": {
        "def": {
          "type": "string",
          "checks": [
            {}
          ]
        },
        "type": "string"
      },
      "nullable": false,
      "hasDefault": false,
      "meta": {},
      "title": "Surname",
      "component": "input"
    },
    {
      "name": "main",
      "kind": "enum",
      "options": [
        {
          "value": "ex",
          "label": "ex"
        },
        {
          "value": "js",
          "label": "js"
        },
        {
          "value": "kt",
          "label": "kt"
        },
        {
          "value": "typescript",
          "label": "typescript"
        },
        {
          "value": "java",
          "label": "java"
        },
        {
          "value": "cobol",
          "label": "cobol"
        },
        {
          "value": "c",
          "label": "c"
        },
        {
          "value": "cpp",
          "label": "cpp"
        },
        {
          "value": "csharp",
          "label": "csharp"
        },
        {
          "value": "fsharp",
          "label": "fsharp"
        }
      ],
      "required": true,
      "constraints": {},
      "children": [],
      "schema": {
        "def": {
          "type": "enum",
          "entries": {
            "ex": "ex",
            "js": "js",
            "kt": "kt",
            "typescript": "typescript",
            "java": "java",
            "cobol": "cobol",
            "c": "c",
            "cpp": "cpp",
            "csharp": "csharp",
            "fsharp": "fsharp"
          }
        },
        "type": "enum",
        "enum": {
          "ex": "ex",
          "js": "js",
          "kt": "kt",
          "typescript": "typescript",
          "java": "java",
          "cobol": "cobol",
          "c": "c",
          "cpp": "cpp",
          "csharp": "csharp",
          "fsharp": "fsharp"
        },
        "options": [
          "ex",
          "js",
          "kt",
          "typescript",
          "java",
          "cobol",
          "c",
          "cpp",
          "csharp",
          "fsharp"
        ]
      },
      "nullable": false,
      "hasDefault": false,
      "meta": {},
      "title": "Main",
      "component": "select"
    },
    {
      "name": "secondary",
      "kind": "enum",
      "options": [
        {
          "value": "ex",
          "label": "ex"
        },
        {
          "value": "js",
          "label": "js"
        },
        {
          "value": "kt",
          "label": "kt"
        },
        {
          "value": "typescript",
          "label": "typescript"
        },
        {
          "value": "java",
          "label": "java"
        },
        {
          "value": "cobol",
          "label": "cobol"
        },
        {
          "value": "c",
          "label": "c"
        },
        {
          "value": "cpp",
          "label": "cpp"
        },
        {
          "value": "csharp",
          "label": "csharp"
        },
        {
          "value": "fsharp",
          "label": "fsharp"
        }
      ],
      "required": true,
      "constraints": {},
      "children": [],
      "schema": {
        "def": {
          "type": "enum",
          "entries": {
            "ex": "ex",
            "js": "js",
            "kt": "kt",
            "typescript": "typescript",
            "java": "java",
            "cobol": "cobol",
            "c": "c",
            "cpp": "cpp",
            "csharp": "csharp",
            "fsharp": "fsharp"
          }
        },
        "type": "enum",
        "enum": {
          "ex": "ex",
          "js": "js",
          "kt": "kt",
          "typescript": "typescript",
          "java": "java",
          "cobol": "cobol",
          "c": "c",
          "cpp": "cpp",
          "csharp": "csharp",
          "fsharp": "fsharp"
        },
        "options": [
          "ex",
          "js",
          "kt",
          "typescript",
          "java",
          "cobol",
          "c",
          "cpp",
          "csharp",
          "fsharp"
        ]
      },
      "nullable": false,
      "hasDefault": false,
      "meta": {},
      "title": "Secondary",
      "component": "select"
    },
    {
      "name": "items",
      "kind": "array",
      "options": [],
      "required": true,
      "constraints": {},
      "children": [],
      "schema": {
        "def": {
          "type": "array",
          "element": {
            "def": {
              "type": "object",
              "shape": {
                "document": {
                  "def": {
                    "type": "string",
                    "checks": [
                      {}
                    ]
                  },
                  "type": "string"
                },
                "name": {
                  "def": {
                    "type": "string",
                    "checks": [
                      {}
                    ]
                  },
                  "type": "string"
                }
              }
            },
            "type": "object"
          }
        },
        "type": "array",
        "element": {
          "def": {
            "type": "object",
            "shape": {
              "document": {
                "def": {
                  "type": "string",
                  "checks": [
                    {}
                  ]
                },
                "type": "string"
              },
              "name": {
                "def": {
                  "type": "string",
                  "checks": [
                    {}
                  ]
                },
                "type": "string"
              }
            }
          },
          "type": "object"
        }
      },
      "nullable": false,
      "hasDefault": false,
      "defaultValue": [],
      "meta": {},
      "title": "Items",
      "component": "list",
      "element": {
        "name": "items[]",
        "kind": "object",
        "options": [],
        "required": true,
        "constraints": {},
        "children": [
          {
            "name": "items[].document",
            "kind": "string",
            "options": [],
            "required": true,
            "constraints": {
              "minLength": 1
            },
            "children": [],
            "schema": {
              "def": {
                "type": "string",
                "checks": [
                  {}
                ]
              },
              "type": "string"
            },
            "nullable": false,
            "hasDefault": false,
            "meta": {},
            "title": "Document",
            "component": "input"
          },
          {
            "name": "items[].name",
            "kind": "string",
            "options": [],
            "required": true,
            "constraints": {
              "minLength": 1
            },
            "children": [],
            "schema": {
              "def": {
                "type": "string",
                "checks": [
                  {}
                ]
              },
              "type": "string"
            },
            "nullable": false,
            "hasDefault": false,
            "meta": {},
            "title": "Name",
            "component": "input"
          }
        ],
        "schema": {
          "def": {
            "type": "object",
            "shape": {
              "document": {
                "def": {
                  "type": "string",
                  "checks": [
                    {}
                  ]
                },
                "type": "string"
              },
              "name": {
                "def": {
                  "type": "string",
                  "checks": [
                    {}
                  ]
                },
                "type": "string"
              }
            }
          },
          "type": "object"
        },
        "nullable": false,
        "hasDefault": false,
        "meta": {},
        "title": "Items",
        "component": "group"
      }
    },
    {
      "name": "oab",
      "kind": "array",
      "options": [],
      "required": true,
      "constraints": {},
      "children": [],
      "schema": {
        "def": {
          "type": "array",
          "element": {
            "def": {
              "type": "object",
              "shape": {
                "document": {
                  "def": {
                    "type": "pipe",
                    "in": {
                      "def": {
                        "type": "string",
                        "checks": [
                          {}
                        ]
                      },
                      "type": "string"
                    },
                    "out": {
                      "def": {
                        "type": "transform"
                      },
                      "type": "transform"
                    }
                  },
                  "type": "pipe",
                  "in": {
                    "def": {
                      "type": "string",
                      "checks": [
                        {}
                      ]
                    },
                    "type": "string"
                  },
                  "out": {
                    "def": {
                      "type": "transform"
                    },
                    "type": "transform"
                  }
                }
              }
            },
            "type": "object"
          }
        },
        "type": "array",
        "element": {
          "def": {
            "type": "object",
            "shape": {
              "document": {
                "def": {
                  "type": "pipe",
                  "in": {
                    "def": {
                      "type": "string",
                      "checks": [
                        {}
                      ]
                    },
                    "type": "string"
                  },
                  "out": {
                    "def": {
                      "type": "transform"
                    },
                    "type": "transform"
                  }
                },
                "type": "pipe",
                "in": {
                  "def": {
                    "type": "string",
                    "checks": [
                      {}
                    ]
                  },
                  "type": "string"
                },
                "out": {
                  "def": {
                    "type": "transform"
                  },
                  "type": "transform"
                }
              }
            }
          },
          "type": "object"
        }
      },
      "nullable": false,
      "hasDefault": false,
      "defaultValue": [],
      "meta": {},
      "title": "Oab",
      "component": "list",
      "element": {
        "name": "oab[]",
        "kind": "object",
        "options": [],
        "required": true,
        "constraints": {},
        "children": [
          {
            "name": "oab[].document",
            "kind": "string",
            "options": [],
            "required": true,
            "constraints": {
              "pattern": "[[0-9]{4,6}\\/[A-Z]"
            },
            "children": [],
            "schema": {
              "def": {
                "type": "string",
                "checks": [
                  {}
                ]
              },
              "type": "string"
            },
            "nullable": false,
            "hasDefault": false,
            "meta": {},
            "title": "Document",
            "component": "input"
          }
        ],
        "schema": {
          "def": {
            "type": "object",
            "shape": {
              "document": {
                "def": {
                  "type": "pipe",
                  "in": {
                    "def": {
                      "type": "string",
                      "checks": [
                        {}
                      ]
                    },
                    "type": "string"
                  },
                  "out": {
                    "def": {
                      "type": "transform"
                    },
                    "type": "transform"
                  }
                },
                "type": "pipe",
                "in": {
                  "def": {
                    "type": "string",
                    "checks": [
                      {}
                    ]
                  },
                  "type": "string"
                },
                "out": {
                  "def": {
                    "type": "transform"
                  },
                  "type": "transform"
                }
              }
            }
          },
          "type": "object"
        },
        "nullable": false,
        "hasDefault": false,
        "meta": {},
        "title": "Oab",
        "component": "group"
      }
    }
  ],
  "errors": null,
  "name": "form",
  "disabled": false
}

When to use useForm

Use when

You need the documented useForm interaction or presentation in a product workflow.

Avoid when

A native HTML element already provides the useForm behavior you need.

Accessibility

Keep the useForm labeled and use its documented keyboard interaction. The component preserves visible focus and communicates state without relying on color alone.