[angular] 양식 검증

3841 단어
html:
(h) *
{{verifyMessages.cost.errorMsg}}
([ngModelOptions]="{standalone: true}"
/* ,  FormControl  FormGroup , {{ f.controls['firstField']?.value }}
  。*/

Validators.ts:(양식 유효성 검사 제어)
/**
 * Created by [email protected] on 3-12.
 */
import {Injectable} from "@angular/core";
import {Validators as angularValidators, AbstractControl} from '@angular/forms';

@Injectable()
export class Validators extends angularValidators {

  /*E-mail*/
  static email = function (control: AbstractControl) {
    return Validators.validatorsByPattern('email', control, '([a-z0-9]*[-_]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\.][a-z]{2,3}([\.][a-z]{2})?');
  };

  /* */
  static phone = function (control: AbstractControl) {
    return Validators.validatorsByPattern('phone', control, '1[0-9]{10,10}');
  };

  /* */
  static chinese = function (control: AbstractControl) {
    return Validators.validatorsByPattern('chinese', control, '[(\u4e00-\u9fa5)]+');
  };

  /* 、 */
  static legallyNamed = function (control: AbstractControl) {
    return Validators.validatorsByPattern('legallyNamed', control, '[A-Za-z0-9_]+');
  };

  /* */
  static number = function (control: AbstractControl) {
    return Validators.validatorsByPattern('number', control, '[0-9]+');
  };

  private static validatorsByPattern = function (name: string, control: AbstractControl, pattern: string) {
    let validatorFn = Validators.pattern(pattern)(control);
    if (validatorFn != null) {
      validatorFn[name] = validatorFn['pattern'];
    }
    return validatorFn;
  };
}

.ts(페이지 로직)
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { FormBuilder } from '@angular/forms';
import { Validators } from "../../../../providers/util/Validators";
@IonicPage()
@Component({
    selector: 'page-rs-detail',
    templateUrl: 'rs-detail.html',
})
export class RsDetailPage {
    myForm: any;
    verifyMessages = {
        'cost': {
            'errorMsg': '',
            'required': ' ',
            'number': ' '
        },
        'defectreason': {
            'errorMsg': '',
            'required': ' '
        }
    };
    constructor(public navCtrl: NavController,
        public navParams: NavParams,
        private formBuilder: FormBuilder) {
            this.myForm = this.formBuilder.group({
                cost: ['', [Validators.required,Validators.number]],
                defectreason: ['', [Validators.required]]
              });
              this.myForm.valueChanges
              .subscribe(data => {
                const verifyMessages = this.verifyMessages;
                for (const field in verifyMessages) {
                  verifyMessages[field].errorMsg = '';
                  const control = this.myForm.get(field);
                  if (control && control.dirty && !control.valid) {
                    const messages = verifyMessages[field];
                    for (const key in control.errors) {
                      messages[key] && (verifyMessages[field].errorMsg += messages[key] + ' ');
                    }
                  }
                }
              });
    }   
}

좋은 웹페이지 즐겨찾기