/jsonToForm

A jquery plugin to turn json schema to form

Primary LanguageJavaScriptMIT LicenseMIT

JsonToForm v2.0 ๐Ÿš€

Modern jQuery plugin that turns JSON Schema-like definitions into beautiful, responsive HTML forms with real-time validation.

License: MIT jQuery TypeScript

Live Demo

โœจ Highlights

  • ๐ŸŽจ Modern UI: clean, responsive (Flexbox/Grid), light/dark ready
  • ๐Ÿ”Ž Real-time validation: instant feedback with friendly hints
  • ๐ŸŒ i18n & RTL: Persian/Farsi and other RTL languages supported
  • ๐Ÿงฉ Rich inputs: string, number, email, tel, url, date, time, textarea, select, checkbox, radio, color, html, object, array
  • ๐Ÿงฑ Modular code: Renderer, Validator, EventHandler, Utils
  • ๐Ÿ›ก๏ธ TypeScript: bundled .d.ts for great IntelliSense

๐Ÿš€ Quick Start

Include jQuery, the compiled plugin, and one of the themes:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <script src="jquery/jquery.min.js"></script>
    <script src="jsonToForm/jsonToForm.v2.js"></script>
    <link href="src/styles/jsonToForm.clean.css" rel="stylesheet" />
    <!-- Or: <link href="src/styles/jsonToForm.modern.css" rel="stylesheet" /> -->
  </head>
  <body>
    <div id="myForm"></div>
    <script>
      const form = $("#myForm").jsonToForm({
        schema: {
          type: "object",
          properties: {
            name: { type: "string", title: "Full Name", minLength: 2 },
            email: { type: "email", title: "Email Address" },
            age: { type: "number", title: "Age", min: 18, max: 100 }
          },
          required: ["name", "email"]
        }
      });

      // Read & validate
      console.log(form.getValue());
      console.log(form.isValid());
    </script>
  </body>
  </html>

๐Ÿงญ API (essentials)

  • getValue() โ†’ returns the current form value
  • setValue(obj) โ†’ sets/replaces form value
  • isValid() โ†’ boolean validity of the whole form
  • validator.getAllErrors() โ†’ list of validation errors

Example:

const form = $("#myForm").jsonToForm(options);
form.setValue({ name: "John Doe" });
console.log(form.getValue(), form.isValid());
console.log(form.validator.getAllErrors());

๐ŸŽจ Theming & RTL

  • Themes: src/styles/jsonToForm.clean.css (simple), src/styles/jsonToForm.modern.css (polished)
  • Dark mode: set data-json-form-theme="dark" on <body>
  • RTL: add dir="rtl" on <html>/<body>/container
<body dir="rtl" data-json-form-theme="dark">
  <!-- your form container -->
</body>

๐Ÿงฑ Project Structure

  • src/ โ†’ modular source (core, renderer, validator, events, utils, styles)
  • jsonToForm/jsonToForm.v2.js โ†’ compiled v2 bundle
  • jsonToForm/jsonToForm.d.ts โ†’ TypeScript definitions
  • v1/ โ†’ legacy v1 plugin, styles, and demos
  • demo-v2.html โ†’ v2 demo

๐Ÿ” Migrating from v1.x

Old usage (v1.x):

$('#myForm').jsonToForm({ schema, value });

New usage (v2):

$('#myForm').jsonToForm({ schema });
$('#myForm').jsonToForm('setValue', value);

For legacy plugin and original demos, see the v1/ folder.

๐Ÿงช Try locally

Run a simple static server and open the demo (PowerShell):

# From the repo root
python -m http.server 8080
# Open in your browser:
# http://localhost:8080/demo-v2.html

๐Ÿ“ License

MIT ยฉ Contributors โ€” see LICENSE