A javascript library to convert form data into a js object which can be converted to json using your favorite json library
<form id="form">
<input name="name" value="Ryan"></input>
</form>
$("#form").formalizeData();
/*
{
name: "Ryan"
}
*/
<input name="name.first" value="Ryan"></input>
<input name="name.last" value="Quinn"></input>
<input name="nested.very.very.deep" value="Hi!"></input>
/*
{
name: {
first: "Ryan",
last: "Quinn"
},
nested: {
very: {
very: {
deep: "Hi!"
}
}
}
}
*/
This is the default syntax for rails projects, for example
<input name="person[first_name]" value="Ryan"></input>
<input name="person[last_name]" value="Quinn"></input>
/*
{
person: {
first_name: "Ryan",
last_name: "Quinn"
}
}
*/
<input name="tag" value="first"></input>
<input name="tag" value="second"></input>
/*
{
tag: ["first", "second"]
}
*/
<input type="radio" name="radioButton" value="asdf" checked>
<input type="radio" name="radioButton" value="not-checked">
<select name="selectField">
<option>Ryan</option>
<option>Mat</option>
</select>
<select name="multipleSelect" multiple>
<option>Ryan</option>
<option>Mat</option>
</select>
/*
{
radioButton: "asdf",
selectField: "Ryan",
multipleSelect: ["Ryan", "Mat"]
}
*/
You can specify the attribute to use when converting to an object. By default we'll use the name attribute. If no attribute is given and no custom attribute is specified, the input will be skipped.
<form>
<input data-custom-name="name" value="ryan"></input>
</form>
$("form").formalizeData({attribute: "data-custom-name"});
/*
{
name: "ryan"
}
*/
<input name="age" data-formalize="number" value="22"></input>
/*
{
age: 22
}
*/
Custom formats allow you to define your own transformation that will happen to an input before we store it
$.formalizeFormat("format-name", function(value) { return value; })
<input name="custom-format" data-formalize="format-name"></input>
Would love any and all help you feel like giving, just please:
- Fork the repo
- Make sure the tests pass (open the SpecRunner.html file)
- Write tests for the functionality you want (They should fail)
- Make them pass
- Push to your fork and submit a pull request