Skip to content

Instantly share code, notes, and snippets.

@dvv
Created September 20, 2010 20:07
Show Gist options
  • Select an option

  • Save dvv/588561 to your computer and use it in GitHub Desktop.

Select an option

Save dvv/588561 to your computer and use it in GitHub Desktop.
function form2obj(){
var r = {};
// for all enabled? inputs
$(':input[name]', $(this)).each(function(i, field){
// get the field
var $field = $(field);
// sanitize field value
var value = $field.val();
// we don't store empty values
if (!value) return;
var type = $field.attr('type');
// TODO: what with selects?
// IE7 reports array for select option!
if (value instanceof Array) value = value[0];
// shim types for <HTML5 browsers
if (type === 'text') type = $field.attr('data-type');
// field type determines its value transformation
if (type === 'checkbox') {
value = $field.is(':checked');
if (!value && !$field.attr('required')) return; // N.B. we ignore unset checkboxes unless they're required
} else if (type === 'number') {
// FIXME: do we need integers?!
value = parseFloat(value);
} else if (type === 'isodate') {
//value = Date.toDB(value);
var x = value;
// four-digit year
x = '0000'.substr(0,4-x.length)+x;
// pattern for partial dates
x += '0000-01-01T00:00:00.000Z'.substring(x.length);
x = Date.parse(x.replace(/-/g,'/').replace('T', ' ').replace(/\.\d+Z$/,''));
if (!isNaN(x) && typeof x === 'number') {
x = new Date(x);
}
if (x instanceof Date) x = x.toJSON();
value = x;
}
//console.log('KV', $field.attr('name'), value, type, typeof value, value instanceof Array);
// put a key-value
var name = $field.attr('name');
var path, p;
// split name to path components
path = name.replace(/\]/g, '').replace(/\[/g, '.').split('.');
var o = r;
// in resulting object sequentially create all path components
while (p = path.shift()) {
if (!o[p]) {
// N.B. handle arrays: if next path component looks like a number -- create array, otherwise object
o[p] = !path.length || isNaN(+path[0]) ? {} : [];
}
// to leaf assign the value
if (!path.length) {
o[p] = value;
}
o = o[p];
}
});
return r;
}
@smith

smith commented Sep 20, 2010

Copy link
Copy Markdown

@dvv

dvv commented Sep 21, 2010

Copy link
Copy Markdown
Author

It misses shim for boolean values (by default browsers report value of "on" for checked checkboxes and nothing for unchecked; this can be fixed, but requires additional markup). It does guesswork for coercion which is not 100% valid.

The gist is supposed to point out the way json-schema validator could work at server-side, allowing for completely JS-less forms with rails-style nested parameters.

NVL, thanks for the link.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment