Skip to content

Instantly share code, notes, and snippets.

@ryanschuhler
Created September 25, 2015 17:22
Show Gist options
  • Select an option

  • Save ryanschuhler/821fdde79571b743e619 to your computer and use it in GitHub Desktop.

Select an option

Save ryanschuhler/821fdde79571b743e619 to your computer and use it in GitHub Desktop.
AUI.add(
'osb-form',
function(A) {
var OSBForm = A.Component.create(
{
ATTRS: {
customRules: {
value: A.Lang.emptyFn
},
fieldClassName: {
value: 'field-wrapper'
},
fieldStrings: {
value: {}
},
formId: {
value: '#osbForm'
},
rules: {
value: {}
}
},
NAME: 'osb-form',
prototype: {
bindUI: function() {
var instance = this;
A.on('load', instance.syncUI, instance);
},
syncUI: function() {
var instance = this;
instance._initializeValidator();
var form = A.one(instance.get('formId'));
instance._delegateFieldFocused(form);
instance._delegateFieldCheck(form);
},
_addCustomRules: function(config) {
var instance = this;
config.RULES.custom = instance.get('customRules');
config.STRINGS.custom = A.config.FormValidator.STRINGS.required;
};
_delegateFieldCheck: function(form) {
var instance = this;
form.delegate(
'change',
function(event) {
var node = event.currentTarget;
instance._fieldCheck(node);
},
'.' + instance.get('fieldClassName')
);
},
_delegateFieldFocused: function(form) {
var instance = this;
form.delegate(
'focus',
function() {
this.addClass('field-focused');
},
'.' + instance.get('fieldClassName')
);
form.delegate(
'blur',
function() {
this.removeClass('field-focused');
},
'.' + instance.get('fieldClassName')
);
},
_fieldCheck: function(node) {
var input = node.one('.field');
if (!input) {
input = node.one('select');
}
if (!input) {
input = node.one('input');
}
if (input.get('value') != "") {
node.addClass('field-filled');
}
else {
node.removeClass('field-filled');
}
},
_initializeValidator: function() {
var instance = this;
var config = new A.FormValidator(
{
boundingBox: instance.get('formId'),
fieldContainer: '.' + instance.get('fieldClassName'),
fieldStrings: instance.get('fieldStrings'),
rules: instance.get('rules'),
validateOnInput: true
}
);
instance._addCustomRules(config);
}
}
}
);
A.OSBForm = OSBForm;
},
'2.0',
{
requires: ['aui-base', 'aui-form-validator', 'event']
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment