Skip to content

Instantly share code, notes, and snippets.

@adieuadieu
Last active March 1, 2018 13:04
Show Gist options
  • Save adieuadieu/8352df09a8dfd159a78f to your computer and use it in GitHub Desktop.
Save adieuadieu/8352df09a8dfd159a78f to your computer and use it in GitHub Desktop.
FormHelper = {
getFormData: function(event, allowEmpty) {
var allowEmpty = 'boolean' === typeof allowEmpty ? allowEmpty : true;
var form = event.target ? $(event.target) : event;
var data = {};
form.find('input[type="hidden"][name], input[type="text"][name], input[name]:checked, textarea[name], select[name]').each(function(i) {
var keys = $(this).attr('name').replace(/\]/g, '').split('[');
var marker;
for (var i = 0; i < keys.length; i++) {
marker = marker || data;
var currentKey = (isNaN(keys[i])) ? keys[i] : parseInt(keys[i]);
var currentKeyIsLast = (i === (keys.length - 1));
var nextKey = null;
if (!currentKeyIsLast) {
nextKey = (isNaN(keys[i+1])) ? keys[i+1] : parseInt(keys[i+1]);
}
if (currentKeyIsLast) {
var value = $(this).val();
if (!isNaN(parseFloat(value))) value = parseFloat(value);
if (!(marker instanceof Array)) {
if ($(this).val().length || allowEmpty) {
marker[currentKey] = value;
}
else delete marker[currentKey];
} else marker.push(value);
} else {
if (typeof marker[currentKey] === 'undefined') {
var nextObject = (typeof nextKey === 'string' && nextKey.length) ? {} : [];
(!(marker instanceof Array)) ? marker[currentKey] = nextObject : marker.push(nextObject);
}
}
marker = marker[currentKey];
}
});
return data;
}
, putFormData: function (form, data, trigger) {
var trigger = 'undefined' !== typeof trigger ? trigger : true
, form = $(form)
, fields = []
, max_depth = 10
;
var recurse = function(d, field, name, depth) {
if (depth > max_depth) {
return;
}
if (typeof d == "object") {
var child = null;
var field = depth > 1 ? field + '['+ name + ']' : name;
for (var item in d) {
child = d[item];
if ('undefined' !== typeof child.join) {
for (var i = child.length - 1; i >= 0; i--) {
fields.push({name: depth > 0 ? field + '['+ item + '][]' : item + '[]', value: child[i]});
}
} else if (typeof child == "object") {
recurse(child, field, item, depth + 1);
} else {
fields.push({name: depth > 1 ? field + '['+ item + ']' : item, value: child});
}
}
} else {
return d;
}
};
recurse(data, '', '', 0);
for (var i = fields.length - 1; i >= 0; i--) {
FormHelper._fieldFucker(fields[i].name, fields[i].value, trigger);
};
if (trigger) form.trigger('change');
}
, _fieldFucker: function (k, v, trigger) {
var trigger = 'undefined' !== typeof trigger ? trigger : true
, inputs = $('input[name="' + k + '"], textarea[name="' + k + '"], select[name="' + k + '"]')
, r
;
if ('undefined' !== typeof v) {
if (null !== v) {
if ('radio' == inputs.attr('type')) r = $(' input[name="' + k + '"][value="' + v + '"]').prop('checked', true);
else if ('checkbox' == inputs.attr('type')) {
r = $('input[name="' + k + '"][value="' + ('boolean' == typeof v ? (v ? 1 : 0) : v) + '"]').prop('checked', true);
}
else r = inputs.val(v);
}
if (trigger) inputs.trigger('change');
return r;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment