【问题标题】:How to check if two fields are equal in Angular / Ionic?如何检查Angular / Ionic中的两个字段是否相等?
【发布时间】:2018-12-13 00:02:51
【问题描述】:

我想检查两个字段的值是否相同,这些字段必须通过参数传递给验证函数,我正在这样做,问题是它无法获取字段的值,它显示为 null,因为我可以正确动态地获取值吗?

我的表单生成器,我正在使用匹配功能来检查手机和确认字段。

this.recharge = this.formBuilder.group({
  cell_phone: ['', Validators.required, Validations.match('cell_phone', 'cell_phone_confirmation')],
  cell_phone_confirmation: ['', [Validators.required]],
  value: ['', Validators.required],
  operator_id: ['', Validators.required]
});

在我的函数中,控制台日志为空:

static match(field1: string, field2: string){
  return (group: FormGroup) => {
    console.log(group.get(field1));
  }
}

【问题讨论】:

    标签: angular ionic-framework ionic3


    【解决方案1】:

    您需要创建一个自定义 formGroup 验证器来检查表单控件值的值并检查主题

    this.recharge = formBuilder.group({
      cell_phone: ['', Validators.required],
      cell_phone_confirmation: ['', Validators.required],
    },
      {
        validator: checkMatchValidator('cell_phone', 'cell_phone_confirmation')
      }
    );
    

    自定义验证器功能

    export function checkMatchValidator(field1: string, field2: string) {
      return function (frm) {
        let field1Value = frm.get(field1).value;
        let field2Value = frm.get(field2).value;
    
        if (field1Value !== '' && field1Value !== field2Value) {
          return { 'notMatch': `value ${field1Value} is not equal to ${field2}` }
        }
        return null;
      }
    }
    

    stackblitz demo??

    【讨论】:

    • 我需要将需要检查的字段传递给函数,因为我还有密码确认字段、电子邮件和函数需要可重用。
    • 你要检查什么单元格并确认tow fileld ??
    • 你能举个例子吗
    • 我有 2 个密码字段和密码确认以及电子邮件和电子邮件确认,两者都与 cell_confirmation 做同样的事情,我不想为每个字段创建 3 个匹配函数。
    • 检查我已经创建了一个函数构建器,因此您可以使用相同的函数来验证两个字段
    【解决方案2】:

    我会使用 root 属性来访问cell_phone_validator 控件

    cell_phone: ['', [Validators.required, this.cellPhoneValidator]]
    
    
    private cellPhoneValidator(control: AbstractControl) {
      if (control.root.get('cell_phone_confirmation') {
        return control.root.get('cell_phone_confirmation').value !== control.value ?
          { cellPhoneValidator: { value: control.value } } : null;
      }
    

    }

    编辑:你想将它用于任何领域,所以让我们让它通用

    cell_phone: ['', [Validators.required, Validations.matchValidator('cell_phone_confirmation')]],
    cell_phone_confirmation: ['', [Validators.required, Validations.matchValidator('cell_phone')]]
    
    private matchValidator(controlValidationName: string): ValidatorFn {
      return (control: AbstractControl) => {
        const controlValidation = control.root.get(controlValidationName);
        if (!controlValidation) {
          return null;
        }
        return controlValidation.value !== control.value ?
          { matchValidator: { value: control.value } } : null;
      }
    }
    

    【讨论】:

    • 我想将此功能以另一种形式与其他字段重用,而不是专门用于移动设备,可以吗?
    • 如果你想重用这个功能,我们应该稍微改变一下
    • 我已经有一个验证类,我在其中创建了自定义函数。
    • @viniciussvl 很高兴为您提供帮助! malbarm 通过在legacyOrOpts formbuilder 的属性中传递验证器提出了另一种解决方案。随意使用你最喜欢的那个;)
    • 很高兴有几种方法可以解决这个问题,谢谢大家。
    【解决方案3】:

    你可以这样试试。

    this.recharge = this.formBuilder.group({
      cell_phone: ['', Validators.required],
      cell_phone_confirmation: ['', [Validators.required]],
      value: ['', Validators.required],
      operator_id: ['', Validators.required]
    },{ validator:  Validations.match });
    
    static match(c: AbstractControl){
      const cellphone = c.get('cell_phone');
      const cellphoneconfirm = c.get('cell_phone_confirmation');
    }
    

    【讨论】:

    • 在 formBuilder 中出现错误提示“'cell_phone' 类型的参数不可分配给 'AbstractControl' 类型的参数”
    • 我需要指定应该验证哪些字段,因为我有密码、电子邮件和单元格字段。
    • 我们正在传递整个表单组实例,您可以访问特定的表单控件。
    猜你喜欢
    • 2015-10-30
    • 1970-01-01
    • 2023-04-04
    • 2015-08-21
    • 1970-01-01
    • 2019-08-27
    • 1970-01-01
    • 2011-04-19
    • 2019-07-27
    相关资源
    最近更新 更多