Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@apipkin
Created December 20, 2009 22:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save apipkin/419cfcb7ca724c12e755 to your computer and use it in GitHub Desktop.
Save apipkin/419cfcb7ca724c12e755 to your computer and use it in GitHub Desktop.
YUI.add("form-values", function(Y){
var _form,
_values
;
Form = 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;
};
Form.setForm = function(selector) {
return Form(selector);
};
Form.getForm = function(){
return _form;
};
Form.getFormValues = function(){
return Form._setFormValues();
};
Form._setFormValues = function(){
_values = {};
if(Form.getForm() !== null) {
_form.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']});
if(Y.Form !== undefined || Y.Form !== null) {
Y.mix(Y.Form, Form);
}else{
Y.Form = Form;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment