代码之家  ›  专栏  ›  技术社区  ›  Miomir Dancevic

反应式角度验证

  •  1
  • Miomir Dancevic  · 技术社区  · 5 年前

    我有一些像这样的反应形式:

    this.form = this.formBuilder.group({
          filter: [''],
          filterReason: [''],
          documentsRequired: [''],
          urgencyReason: [''],
          reportRequestedDate: [''],
          urgencyDate: [''],
          urgencyRemarks: ['']
        });
    

    像这样的价值观:

    validation = false;
    

    我的问题是我需要切换它的值 验证 在HTML中,基于该if 验证=真 我需要向如下字段添加验证:

    this.form = this.formBuilder.group({
          filter: [''],
          filterReason: [''],
          documentsRequired: [''],
          urgencyReason: [''],
          reportRequestedDate: ['', Validation.required],
          urgencyDate: ['', Validation.required],
          urgencyRemarks: ['', Validation.required]
        });
    

    如何实现这一点,即使我在呈现组件时有init表单?

    3 回复  |  直到 5 年前
        1
  •  3
  •   yurzui    5 年前

    角度暴露我们 setValidators 方法对 AbstractControl 因此,您可以根据您的条件更改验证程序集:

    onValidationChanged(validation: boolean) {
      ['reportRequestedDate', 'urgencyDate', 'urgencyRemarks'].forEach(name => {
        this.form.get(name).setValidators(validation ? Validators.required : null);
      });
    }
    
        2
  •  1
  •   jo_va    5 年前

    这里是一个 complete StackBlitz 具有 反应形式 动态验证 .

    我们的想法是在 validation 属性,以便在每次设置属性时使用 myControl.setValidators(validate ? [Validators.required] : null) 然后通过调用更新其有效性状态 myControl.updateValueAndValidity()

    两种方法对您的 FormControl 在这里:

    • StValueActuffScript()
    • 更新值和有效性()
    import { Component } from '@angular/core';
    import { FormBuilder, FormControl, Validators } from '@angular/forms';
    
    @Component({
      selector: 'my-app',
      templateUrl: './app.component.html',
      styleUrls: [ './app.component.css' ]
    })
    export class AppComponent  {
      form: any;
      private _validation = true;
    
      constructor(private formBuilder: FormBuilder) {
        this.form = this.formBuilder.group({
            filter: ['Hello'],
            filterReason: ['Test'],
            documentsRequired: ['Document'],
            urgencyReason: ['Nothing'],
            reportRequestedDate: [''],
            urgencyDate: ['Today'],
            urgencyRemarks: ['']
          });
          this.setValidators(this.validation);
      }
    
      set validation(validate: boolean) {
        this._validation = validate;
        this.setValidators(validate);
      }
      get validation(): boolean {
        return this._validation;
      }
    
      setValidators(validate: boolean): void {
        this.reportRequestedDate.setValidators(validate ? [Validators.required] : null);
        this.reportRequestedDate.updateValueAndValidity();
    
        this.urgencyDate.setValidators(validate ? [Validators.required] : null);
        this.reportRequestedDate.updateValueAndValidity();
    
        this.urgencyRemarks.setValidators(validate ? [Validators.required] : null);
        this.urgencyRemarks.updateValueAndValidity();
      }
    
      onSubmit(): void {
        if (this.form.valid) {
          console.log(this.form.value);
        }
      }
    
      get reportRequestedDate(): FormControl {
        return this.form.get('reportRequestedDate') as FormControl;
      }
    
      get urgencyDate(): FormControl {
        return this.form.get('urgencyDate') as FormControl;
      }
    
      get urgencyRemarks(): FormControl {
        return this.form.get('urgencyRemarks') as FormControl;
      }
    }
    

    希望有帮助!

        3
  •  0
  •   Baruch G.    5 年前

    在HTML中:

    <button  (click)="toggleValidation();">toggle validation
    
    </button>
    

    在你的TS:

        toggleValidation(validation : boolean)
    Object.keys(this.form).forEach(key=> {
    this.form.get(key).setValidators(validation && key.*your condition* ? Validators.required : null); });}