-
-
Save apipkin/419cfcb7ca724c12e755 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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