UNPKG

fe-validate

Version:

angular 2+ form validate

1 lines 8.48 kB
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("@angular/core"),require("@angular/forms"),require("@angular/common")):"function"==typeof define&&define.amd?define(["@angular/core","@angular/forms","@angular/common"],t):"object"==typeof exports?exports["fe-modal-core"]=t(require("@angular/core"),require("@angular/forms"),require("@angular/common")):e["fe-modal-core"]=t(e["@angular/core"],e["@angular/forms"],e["@angular/common"])}(this,function(e,t,r){return function(e){function t(o){if(r[o])return r[o].exports;var n=r[o]={i:o,l:!1,exports:{}};return e[o].call(n.exports,n,n.exports,t),n.l=!0,n.exports}var r={};return t.m=e,t.c=r,t.d=function(e,r,o){t.o(e,r)||Object.defineProperty(e,r,{configurable:!1,enumerable:!0,get:o})},t.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(r,"a",r),r},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="/",t(t.s=4)}([function(t,r){t.exports=e},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var o=r(0);t.CUSTOM_ERROR_MESSAGES=new o.InjectionToken("form-validation custom error messages")},function(e,t,r){"use strict";var o=this&&this.__decorate||function(e,t,r,o){var n,i=arguments.length,a=i<3?t:null===o?o=Object.getOwnPropertyDescriptor(t,r):o;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)a=Reflect.decorate(e,t,r,o);else for(var s=e.length-1;s>=0;s--)(n=e[s])&&(a=(i<3?n(a):i>3?n(t,r,a):n(t,r))||a);return i>3&&a&&Object.defineProperty(t,r,a),a},n=this&&this.__metadata||function(e,t){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(e,t)},i=this&&this.__param||function(e,t){return function(r,o){t(r,o,e)}};Object.defineProperty(t,"__esModule",{value:!0});var a=r(0),s=r(7),u=r(1),c=function(){function e(e){void 0===e&&(e=[]),this.customErrorMessages=e,this.defaultErrors=s.DEFAULT_ERRORS}return Object.defineProperty(e.prototype,"errorMessages",{get:function(){return this.mergeArray(this.customErrorMessages,this.defaultErrors)},enumerable:!0,configurable:!0}),e.prototype.mergeArray=function(e,t){void 0===e&&(e=[]),void 0===t&&(t=[]);for(var r=[],o=0,n=t;o<n.length;o++){for(var i=n[o],a=!1,s=0,u=e;s<u.length;s++){var c=u[s];i.error===c.error&&(a=!0)}a||r.push(i)}return r.concat(e||[])},e=o([a.Injectable(),i(0,a.Inject(u.CUSTOM_ERROR_MESSAGES)),n("design:paramtypes",[Array])],e)}();t.ErrorMessageService=c},function(e,r){e.exports=t},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var o=r(1);t.CUSTOM_ERROR_MESSAGES=o.CUSTOM_ERROR_MESSAGES;var n=r(5);t.FormValidationModule=n.FormValidationModule},function(e,t,r){"use strict";var o=this&&this.__decorate||function(e,t,r,o){var n,i=arguments.length,a=i<3?t:null===o?o=Object.getOwnPropertyDescriptor(t,r):o;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)a=Reflect.decorate(e,t,r,o);else for(var s=e.length-1;s>=0;s--)(n=e[s])&&(a=(i<3?n(a):i>3?n(t,r,a):n(t,r))||a);return i>3&&a&&Object.defineProperty(t,r,a),a};Object.defineProperty(t,"__esModule",{value:!0});var n=r(6),i=r(0),a=r(2),s=r(8),u=r(9),c=function(){function e(){}return t=e,e.forRoot=function(){return{ngModule:t,providers:[]}},e=t=o([i.NgModule({declarations:[u.FormValidationDirective,s.FormValidateComponent],imports:[n.CommonModule],providers:[a.ErrorMessageService],exports:[u.FormValidationDirective,s.FormValidateComponent]})],e);var t}();t.FormValidationModule=c},function(e,t){e.exports=r},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.DEFAULT_ERRORS=[{error:"required",format:function(e){return e+"未填写"}},{error:"minlength",format:function(e,t){return e+"必须至少"+t.requiredLength+"字符"}},{error:"maxlength",format:function(e,t){return e+"必须不大于"+t.requiredLength+"字符"}},{error:"max",format:function(e,t){return e+" 必须不大于 "+t.max}},{error:"min",format:function(e,t){return e+" 必须不得少于 "+t.min}},{error:"email",format:function(e,t){return"无效的邮件地址"}}]},function(e,t,r){"use strict";var o=this&&this.__decorate||function(e,t,r,o){var n,i=arguments.length,a=i<3?t:null===o?o=Object.getOwnPropertyDescriptor(t,r):o;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)a=Reflect.decorate(e,t,r,o);else for(var s=e.length-1;s>=0;s--)(n=e[s])&&(a=(i<3?n(a):i>3?n(t,r,a):n(t,r))||a);return i>3&&a&&Object.defineProperty(t,r,a),a},n=this&&this.__metadata||function(e,t){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(e,t)};Object.defineProperty(t,"__esModule",{value:!0});var i=r(0),a=r(3),s=r(2),u=function(){function e(e,t){this.elRef=e,this.errorMessageService=t,this.customErrorMessages=[],this.validationDisabled=!1}return Object.defineProperty(e.prototype,"hasErrors",{get:function(){return this.FormControlNames.some(function(e){return!e.valid&&e.dirty&&e.touched})&&!this.validationDisabled},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"hasSuccess",{get:function(){return!this.FormControlNames.some(function(e){return!e.valid})&&this.FormControlNames.some(function(e){return e.dirty&&e.touched})&&!this.validationDisabled},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"label",{get:function(){var e=this.elRef.nativeElement.querySelector(".form-control").getAttribute("placeholder");return e?e.trim():""},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"isDirtyAndTouched",{get:function(){return this.FormControlNames.some(function(e){return e.dirty&&e.touched})},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"errorMessages",{get:function(){return this.errorMessageService.mergeArray(this.customErrorMessages,this.errorMessageService.errorMessages)},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"messages",{get:function(){var e=this,t=[];return!this.isDirtyAndTouched||this.validationDisabled?t:(this.FormControlNames.filter(function(e){return!e.valid}).forEach(function(r){r.errors&&Object.keys(r.errors).forEach(function(o){var n=e.errorMessages.find(function(e){return e.error===o});n&&t.push(n.format(e.label,r.errors[o]))})}),t)},enumerable:!0,configurable:!0}),o([i.ContentChildren(a.FormControlName),n("design:type",i.QueryList)],e.prototype,"FormControlNames",void 0),o([i.Input(),n("design:type",Array)],e.prototype,"customErrorMessages",void 0),o([i.Input(),n("design:type",Boolean)],e.prototype,"validationDisabled",void 0),o([i.HostBinding("class.has-error"),n("design:type",Object),n("design:paramtypes",[])],e.prototype,"hasErrors",null),o([i.HostBinding("class.has-success"),n("design:type",Object),n("design:paramtypes",[])],e.prototype,"hasSuccess",null),e=o([i.Component({selector:".form-group",template:'\n\t\t<ng-content></ng-content>\n\t\t<div class="help-block" *ngFor="let message of messages">{{message}}</div>\n\t'}),n("design:paramtypes",[i.ElementRef,s.ErrorMessageService])],e)}();t.FormValidateComponent=u},function(e,t,r){"use strict";var o=this&&this.__decorate||function(e,t,r,o){var n,i=arguments.length,a=i<3?t:null===o?o=Object.getOwnPropertyDescriptor(t,r):o;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)a=Reflect.decorate(e,t,r,o);else for(var s=e.length-1;s>=0;s--)(n=e[s])&&(a=(i<3?n(a):i>3?n(t,r,a):n(t,r))||a);return i>3&&a&&Object.defineProperty(t,r,a),a},n=this&&this.__metadata||function(e,t){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(e,t)};Object.defineProperty(t,"__esModule",{value:!0});var i=r(0),a=r(3),s=function(){function e(){this.validSubmit=new i.EventEmitter}return e.prototype.onSubmit=function(){this.markAsTouchedAndDirty(this.formGroup),this.formGroup.valid&&this.validSubmit.emit(this.formGroup.value)},e.prototype.markAsTouchedAndDirty=function(e){var t=this;Object.keys(e.controls).forEach(function(r){e.controls[r]instanceof a.FormGroup?t.markAsTouchedAndDirty(e.controls[r]):(e.controls[r].markAsDirty(),e.controls[r].markAsTouched(),e.controls[r].updateValueAndValidity())})},o([i.Input(),n("design:type",a.FormGroup)],e.prototype,"formGroup",void 0),o([i.Output(),n("design:type",Object)],e.prototype,"validSubmit",void 0),o([i.HostListener("submit"),n("design:type",Function),n("design:paramtypes",[]),n("design:returntype",void 0)],e.prototype,"onSubmit",null),e=o([i.Directive({selector:"form"}),n("design:paramtypes",[])],e)}();t.FormValidationDirective=s}])});