UNPKG

form-request

Version:
1 lines 1.74 kB
var FormRequest=function(){function FormRequest(t){$("[error-message]").hide();var e={date:"",email:"",file:"",hidden:"",number:"",password:"",text:""},r={checkbox:'prop("checked")',radio:'prop("checked")'};this.form=t,this.input_val=e,this.input_prop=r,this.hideError()}return FormRequest.prototype=Object.create(Validator.prototype),FormRequest.prototype.run=function(){for(var t=this.form.find("[rules]"),e=0;e<t.length;e++){var r=this.rules(t[e]);if(r.fails)return this.showError(t[e],r.message),!1;this.hideError(t[e])}},FormRequest.prototype.getValue=function(t){return $(t).attr("type")in this.input_val?$(t).val():$(t).attr("type")in this.input_prop?$(t).prop("checked"):void alert("Error: The type {"+$(t).attr("type")+"} ins't suported.")},FormRequest.prototype.getName=function(t){return $(t).attr("request")},FormRequest.prototype.getRules=function(t){return $(t).attr("rules")},FormRequest.prototype.showError=function(t,e){return $("html, body").animate({scrollTop:$('[error="'+this.getName(t)+'"]').offset().top-100},"slow"),$('[error="'+this.getName(t)+'"]').html(e).fadeIn(300)},FormRequest.prototype.hideError=function(t){return t?$('[error="'+this.getName(t)+'"]').hide().html(""):$("[error]").hide().html("")},FormRequest.prototype.generateCall=function(input_value,callback){var params=callback.split(":")[1],call="this."+callback.split(":")[0]+"(";return call+="string"==typeof input_value&&""!==input_value?'"'+input_value+'"':input_value,void 0!==params&&(call+=', "'+params+'"'),eval(call+")")},FormRequest.prototype.rules=function(t){var e=this.getRules(t).split("|");for(var r in e)if(!this.generateCall(this.getValue(t),e[r]))return{fails:!0,message:this.getErrors(this.getName(t))};return{fails:!1}},FormRequest}();