|
|
!function(a,b,c,d){a.fn.form=function(b,c){var e,f=a(this),g=a.extend(!0,{},a.fn.form.settings,c),h=a.extend({},a.fn.form.settings.defaults,b),i="."+g.namespace,j=("module-"+g.namespace,f.selector||""),k=(new Date).getTime(),l=[],m=arguments[0],n="string"==typeof m,o=[].slice.call(arguments,1);return f.each(function(){var b,c=a(this),p=a(this).find(g.selector.field),q=a(this).find(g.selector.group),r=a(this).find(g.selector.message),s=(a(this).find(g.selector.prompt),a(this).find(g.selector.submit)),t=[],u=this,v=c.data("module-"+g.namespace),w=g.namespace,x=g.metadata,y=g.className,z=g.error;b={initialize:function(){b.verbose("Initializing form validation",c,h,g),g.keyboardShortcuts&&p.on("keydown"+i,b.event.field.keydown),c.on("submit"+i,b.validate.form),p.on("blur"+i,b.event.field.change),s.on("click"+i,b.submit)},destroy:function(){c.off(w)},refresh:function(){p=c.find(g.selector.field)},submit:function(){b.verbose("Submitting form",c),c.submit()},event:{field:{keydown:function(c){var d=a(this),e=c.which,f={enter:13,escape:27};return e==f.escape&&(b.verbose("Escape key pressed blurring field"),d.blur()),e==f.enter&&d.is(g.selector.input)?(b.debug("Enter key pressed, submitting form"),s.addClass(y.down),d.one("keyup"+i,b.event.field.keyup),c.preventDefault(),!1):void 0},keyup:function(){b.verbose("Doing keyboard shortcut form submit"),s.removeClass(y.down),b.submit()},change:function(){var c=a(this),d=c.closest(q);d.hasClass(y.error)?(b.debug("Revalidating field",c,b.get.validation(c)),b.validate.field(b.get.validation(c))):"change"==g.on&&b.validate.field(b.get.validation(c))}}},get:{field:function(c){return b.verbose("Finding field with identifier",c),p.filter("#"+c).size()>0?p.filter("#"+c):p.filter('[name="'+c+'"]').size()>0?p.filter('[name="'+c+'"]'):p.filter("[data-"+x.validate+'="'+c+'"]').size()>0?p.filter("[data-"+x.validate+'="'+c+'"]'):a("<input/>")},validation:function(c){var d;return a.each(h,function(a,e){b.get.field(e.identifier).get(0)==c.get(0)&&(d=e)}),d||!1}},has:{field:function(a){return b.verbose("Checking for existence of a field with identifier",a),p.filter("#"+a).size()>0?!0:p.filter('[name="'+a+'"]').size()>0?!0:p.filter("[data-"+x.validate+'="'+a+'"]').size()>0?!0:!1}},add:{prompt:function(a,c){var d=b.get.field(a.identifier),e=d.closest(q),f=e.find(g.selector.prompt),h=0!==f.size();b.verbose("Adding inline validation prompt"),e.addClass(y.error),g.inlineError&&(h||(f=g.templates.prompt(c),f.appendTo(e).hide()),f.html(c[0]),f.is(":not(:visible)")&&f.fadeIn(g.animateSpeed))},errors:function(a){b.debug("Adding form error messages",a),r.html(g.templates.error(a))}},remove:{prompt:function(a){var c=b.get.field(a.identifier),d=c.closest(q),e=d.find(g.selector.prompt);d.removeClass(y.error),g.inlineError&&e.hide()}},validate:{form:function(d){var e=!0;return t=[],a.each(h,function(a,c){b.validate.field(c)||(e=!1)}),e?(b.debug("Form has no validation errors, submitting"),c.removeClass(y.error).addClass(y.success),a.proxy(g.onSuccess,this)(d),void 0):(b.debug("Form has errors"),c.addClass(y.error),g.inlineError||b.add.errors(t),a.proxy(g.onFailure,this)(t))},field:function(c){var e=b.get.field(c.identifier),f=!0,h=[];return c.rules!==d&&a.each(c.rules,function(a,d){b.has.field(c.identifier)&&!b.validate.rule(c,d)&&(b.debug("Field is invalid",c.identifier,d.type),h.push(d.prompt),f=!1)}),f?(b.remove.prompt(c,h),a.proxy(g.onValid,e)(),!0):(t=t.concat(h),b.add.prompt(c,h),a.proxy(g.onInvalid,e)(h),!1)},rule:function(e,f){var h,i,j=b.get.field(e.identifier),k=f.type,l=j.val(),m=/\[(.*?)\]/i,n=m.exec(k),o=!0;return n!==d&&null!==n?(h=n[1],i=k.replace(n[0],""),o=a.proxy(g.rules[i],c)(l,h)):o="checked"==k?j.filter(":checked").size()>0:g.rules[k](l),o}},setting:function(b,c){return c===d?g[b]:(a.isPlainObject(b)?a.extend(!0,g,b):g[b]=c,void 0)},internal:function(c,e){return e===d?b[c]:(a.isPlainObject(c)?a.extend(!0,b,c):b[c]=e,void 0)},debug:function(){g.debug&&(g.performance?b.performance.log(arguments):b.debug=Function.prototype.bind.call(console.info,console,g.moduleName+":"))},verbose:function(){g.verb
|