Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create a html form using a JSON definition?

Tags:

I am looking for a javascript lib which enables me to store (html) forms in JSON format, with a bit of intelligence built into validate inputs client side. Something like:

function FormController(){   this.user = {     name: 'John Smith',     address:{line1: '123 Main St.', city:'Anytown', state:'AA', zip:'12345'},     contacts:[{type:'phone', value:'1(234) 555-1212'}]   };   this.state = /^\w\w$/;   this.zip = /^\d\d\d\d\d$/; } 

(This is taken from http://docs.angularjs.org/#!cookbook.form ). It's almost what I want, as it offers basic client side validation with regular expressions and you can provide defaults but it appears to still need you to create the HTML form.

I want something that only requires the definition. Any ideas?

like image 285
Ed_ Avatar asked Apr 01 '11 03:04

Ed_


People also ask

Can you write HTML in JSON?

This article is going to assume that you're trying to insert HTML directly inside the JSON file. The problem with HTML inside JSON is that HTML elements have properties that must use double quotes. But as soon as you insert double quotes it's going to cause syntax error because JSON can only do double quotes.

How fetch data from JSON to HTML?

The jQuery code uses getJSON() method to fetch the data from the file's location using an AJAX HTTP GET request. It takes two arguments. One is the location of the JSON file and the other is the function containing the JSON data. The each() function is used to iterate through all the objects in the array.

Can form data be JSON?

The FormData API doesn't directly convert form values to JSON, but we can get there by using the entries method and passing its return value to Object. fromEntries , which returns a plain JavaScript object. This is compatible with JSON.


1 Answers

In shameless self promotion I would also like to mention my jQuery.dForm plugin. It's been around for a while now and support the jQuery validation plugin, jQuery UI and is easily extensible. This is how it looks:

var formdata = {     "action" : "index.html",     "method" : "get",     "elements" :      [         {             "name" : "textfield",             "caption" : "Label for textfield",             "type" : "text",             "value" : "Hello world"         },         {             "type" : "submit",             "value" : "Submit"         }     ]            }; $("#myform").buildForm(formdata);  // Or to load the form definition via AJAX $("#myform").buildForm("http://example.com/myform.json"); 
like image 53
Daff Avatar answered Sep 17 '22 18:09

Daff