FormerJS allows you to automatically build simple forms, based on a JSON object descriptor. The form will be attached to a root element, to be passed to the constructor. You may also attach every input element of the form to different DOM elements in your page, provided they have a unique id attribute. Demo here
- load former.js with a
<script>
tag in your page - create an instance of
Former
, passing the source JSON data (file, Object or JSON string) and the root element to attach the form tovar rootElement = document.getElementById("myDivID");
var f = new Former("form_descriptor.json", rootElement);
The JSON descriptor object has some mandatory properties and some optional ones. A minimal setup would require the following:
[
{
"method":"POST",
"action":"http://path/to/data/endpoint.php",
"submitSuccessCallBack":"yourSubmitSuccessFunctionName",
"submitErrorCallBack":"yourSubmitErrorFunctionName",
"fields":[
{
"id":"name",
"type":"text"
},
{
"id":"pwd",
"type":"password"
},
{
"id":"sub",
"type":"submit",
"value":"submit"
}
]
}
]
Once loaded, Former will build the form and add it to the DOM. Upon submit, Former will:
- make a XHR request with the defined
method
to theaction
url - send the urlencoded form data to the endpoint (in the key-values pairs, key will be the
id
attribute of the input field) - trigger the
submitSuccessCallBack
or thesubmitErrorCallBack
, based on the outcome.
Make sure to define your callbacks and pass their names in the JSON object, in order to trigger the appropriate actions after submittting.
In the fields property of the JSON data, every Object
represents a single <input>
element.
Every property in the Object will be set as attribute in the element. For instance, the above:
{
"id":"name",
"type":"text"
}
generates
<input type="text id="name" />
Therefore you may add custom CSS classes or any other attribute to the element. Example:
{
"id":"name",
"type":"text",
"class":"myCustomStyle",
"required":""
}
outputs:
<input type="text id="name" class="myCustomStyle" required />
You may add the following special (and optional) properties to the JSON object, inside individual fields objects:
"fjs-validate":"someFunctionName"
upon submit, the field will be validated invoking someFunctionName()
(you will have to define the function in your code)
"fjs-parent":"someElementID"
the input element will be attached to the DOM element with id="someElementID"
, rather than to the one passed in the Former constructor
"fjs-fieldName":"someName"
upon submit, when form data is collected and urlencoded, someName
will be used instead of the input id
attribute, as key in the key-value pair
Custom properties are NOT set as attributes in the generated DOM element.
{
"id":"mySelect",
"type":"select",
"fjs-options":[
{"data":"", "text":"Choose a product"},
{"data":"p1", "text":"Product One"},
{"data":"p2", "text":"Product Two", "selected":true}
]
}
To create a select element, specify a fjs-options
property (array of objects) in your JSON field element.
{
"type":"label",
"for":"myInputID",
"fjs-content":"Name"
}
A label element has a fjs-content
property that defines the label text. You may optionally add an id
property if you want to append an input element to the label (i.e. checkboxes or radio buttons)
{
"id":"comment",
"type":"textarea",
"rows":"5"
}
Textareas are quite straightforward.
Since every property in the Object will be set as attribute in the element, you may define inline styles and callbacks directly in the JSON Object, as follows:
{
"id":"name",
"type":"text",
"style":"width:100px;height:200px;",
"onclick":"myOnClickCallback(this)"
}
generates
<input type="text id="name" style="width:100px;height:200px;" onclick="myOnClickCallback(this)" />
This is useful for onFocus/onBlur events or onChange events. If you use this method, you must include the parenthesis in the callback, as in the above example.
There are two ways of validating a field.
- The easy way (does NOT work for checkboxes and radio buttons) is to set a
required
property in your JSON Object and (optionally) afjs-emptyFieldCallBack
to invoke a custom function if the field is empty.
{
"id":"name",
"type":"text",
"required":"",
"fjs-emptyFieldCallBack":"fillName"
}
The above will automatically invoke fillName()
(defined in your code) and give focus to the input field if it has no value.
Please note that if required
is not set, then fjs-emptyFieldCallBack
will never be invoked!
- The custom way is to set a
fjs-validate
property in your JSON Object and the name of a custom validation function.
{
"id":"name",
"type":"text",
"fjs-validate":"validateName"
}
When you define validateName
in your code, keep in mind that it will receive one parameter (a reference to the element being validated) and will have to return either true or false.
function validateName(el) {
if (el.value === '') {
alert("please enter a name");
el.focus();
return false;
} else {
return true;
}
}
Although they don't really need to, the easy and custom methods can be used concurrently on the same element. The library will first check the easy way and if validation passes, will proceed to the custom one.
This is a work in progress, guys. Don't expect it to be perfect or to immediately suit all your needs. It's an experiment, so treat it as such :)