angular - Angular2 solution for validation of form with validation class or interface -


i need have validation class use many times in separate section, suppose have forma has lot of elements, use same form in multiple sections, validation want have validation class forma when want validate form anywhere, should have validation class avoid different validation rules same form , have unique rules form. have searched , found using validation class each element , using class, this:

import {formcontrol} '@angular/forms';  import {directive,forwardref} '@angular/core';  import {ng_validators} '@angular/forms'; export function validatezip(c: formcontrol) {    let zip_regexp:regexp = new regexp('[a-za-z]{5}');     return zip_regexp.test(c.value) ? null : {      validatezip: {        valid: false      }    };  }   @directive({    selector: '[validatezip][ngmodel],[validatezip][formcontrol]',    providers: [     {       provide: ng_validators, useexisting: forwardref(() =>         zipvalidator), multi: true    }    ]  })  export class zipvalidator {     validator: function = validatezip;     validate(c: formcontrol) {      return this.validator(c);    }  } 

and form use validation:

<form>   <input type="text" name="zip" ngmodel validatezip> </form> 

but need have validation class contain validation rules of form , use that.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -