How to Use the org.brutusin:json-forms Library

May 24, 2024 | Programming

Welcome to the world of dynamic web forms! Today, we will be diving into how to utilize the org.brutusin:json-forms JavaScript library, which skillfully generates HTML forms from JSON Schemas. Whether you’re looking to streamline your form creation process or tap into the benefits of dynamic schemas, this guide will walk you through everything you need to know. Let’s get started!

Features

  • Dynamic schemas support
  • Extensible and customizable
  • No external libraries needed
  • Built-in validation
  • Support for multiple forms per document

Usage

To start using the json-forms library, you first need to include the necessary library dependencies in your HTML file:

<link rel="stylesheet" href="dist/css/brutusin-json-forms.min.css">
<script src="dist/js/brutusin-json-forms.min.js"></script>
<script src="dist/js/brutusin-json-forms-bootstrap.min.js"></script>

Next, create a JavaScript object representing your schema structure. For instance, if your schema is a simple boolean type, it can be created as follows:

var schema = {
    type: "boolean"
};
var BrutusinForms = brutusin["json-forms"];
var bf = BrutusinForms.create(schema);

Now you can render the form inside a designated container. This can optionally include preloaded JSON initial data:

var container = document.getElementById("container");
bf.render(container, data);

Understanding the Code Through Analogy

Think of the json-forms library like an experienced chef in a bustling kitchen. The chef takes the ingredients (the schema) and prepares a delicious dish (the HTML form) based on the recipe provided. The chef needs the right ingredients in the right amounts (type definitions), and they must work seamlessly with the kitchen setup (the container) to deliver the final plate to the customer (the user). Just as each ingredient is crucial to the recipe, every part of the schema contributes to the resulting form. This relationship allows for dynamic modifications—like adjusting spice levels based on customer feedback. In our case, it’s the user’s input that can alter the form shape and content.

Dynamic Schemas

With json-forms, dynamic schemas allow you to create forms that adapt based on user data. This is achieved using the dependsOn property, which builds a subschema dependency graph, allowing for flexibility and simplicity in form design.

API Overview

The library provides various static and instance members for effective form creation and management:

Static Members

  • BrutusinForms.create(schema) – Factory method for creating instances.
  • BrutusinForms.addDecorator(f(htmlElement, schema)) – Register a callback once an HTML element has been rendered.
  • BrutusinForms.postRender(instance) – Callback function after a BrutusinForms instance is rendered.
  • BrutusinForms.instances – Array of all the form instances created.

Instance Members

  • bf.render(container, data) – Renders the form in the specified container with preloaded data.
  • bf.validate() – Validates user input and returns true if valid.
  • bf.getData() – Retrieves the data entered by the user.
  • bf.schemaResolver(schemaIdArray, data) – Resolves subschemas for dynamic schemata.

Troubleshooting

If you run into any issues, here are some ideas to guide you:

  • Ensure all library dependencies are properly linked in your HTML file.
  • Double-check that your schema is correctly defined as a valid JSON object.
  • Verify the container ID matches the ID used in the JavaScript to render the form.
  • If the dynamic features aren’t functioning properly, review the use of the dependsOn property and schema definitions.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Additional Resources

Check out the library’s CDN for easy integration into your projects.

Conclusion

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox