Skip to content

Instantly share code, notes, and snippets.

@apipkin
Created February 1, 2010 16:45
Show Gist options
  • Save apipkin/291811 to your computer and use it in GitHub Desktop.
Save apipkin/291811 to your computer and use it in GitHub Desktop.
YUI.add("form-values", function(Y){
var _form,
_values
;
Y.Form = {
setForm : function(selector) {
if(selector !== null) {
var node = Y.one(selector);
if(node && node.get('nodeName').toLowerCase() === 'form') {
_form = node;
return _form;
}
}
_form = null;
return _form;
},
getForm : function() {
return _form;
},
getFormValues : function(){
return this._setFormValues();
},
_setFormValues : function(){
_values = {};
var f = this.getForm();
if(f !== null) {
f.get('elements').each(function(field){
var type = field.get('nodeName') + ':' + (field.get('type') || ''),
name = field.get('name'),
value;
switch (type.toLowerCase()) {
case 'input:text' : // fall through intentional
case 'input:hidden' :
case 'textarea:' :
case 'select:' :
value = field.get('value');
break;
case 'input:radio' : // fall through intentional
case 'input:checkbox' :
value = field.get('checked') ? field.get('value') : undefined;
break;
}
if(value !== undefined) {
if (name in _values) {
if(!Y.Lang.isArray(_values[name])) {
_values[name] = [_values[name]];
}
_values[name].push(value);
}else{
_values[name] = value;
}
}
});
}
return _values;
}
};
}, '0.1', {requires : ['node']});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment