How can i implement my custom validator on Angular 2?

前端 未结 1 582
臣服心动
臣服心动 2021-01-24 20:52

How can I implement my custom Validator in Angular 2?

I found this plunker:

constructor(private fb: FormBuilder) {
    this.form = fb.group({
        \'         


        
1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-24 21:13

    // Control handling

              this.form = fb.group({
                'singleSelection': ['Rio', this.text({min: 10})]
              });
    

    // Text function to handle min value

               public static text(config = {}): ValidatorFn {
                 return (control: Control): {} => {
                    if(!control) {
                       return null;
                    }
    
                  let c: string = control.value;
    
                if(config.min) {
                  if(c.length < config.min) {
                      return {
                      "This field must have at least " + config.min + " characters."
                      };
                   }
                 }
               }}
    

    0 讨论(0)
提交回复
热议问题