简体   繁体   English

Angular 自定义验证器在值为 null 时返回 FormControl

[英]Angular custom validator returning the FormControl when value is null

I have an Angular Reactive form group using a custom validator.我有一个使用自定义验证器的 Angular 反应式表单组。 I am using 2 date pickers and want to confirm if a user selects a date from 1 they have to select a date from the other.我正在使用 2 个日期选择器,并想确认用户是否从 1 中选择一个日期,他们必须从另一个日期选择 select。 I am having issues doing this due to null checks.由于 null 检查,我在执行此操作时遇到问题。 So in my testing I am just filling out the first date picker and leaving the second untouched - but this is causing strange issues.所以在我的测试中,我只是填写了第一个日期选择器,而第二个保持不变——但这会导致奇怪的问题。 the unfilled out Form Control has a value: null but when I console log that value I am getting back the formControl未填写的表单控件有一个值: null 但是当我控制台记录该值时,我将取回 formControl

this.form = this.fb.group({
      reservationNumber: this.fb.control(null),
      reservationName: this.fb.control(null),
      dateGroup: this.fb.group(
        {
          beginDate: [this.fb.control(null)],
          endDate: [this.fb.control(null)],
        },
        { validators: checkIfEndDateAfterStartDate }
      ),
      organizationName: this.fb.control(null),
      firstName: this.fb.control(null),
      lastName: this.fb.control(null),
   
    });
  }

here is my custom Validator这是我的自定义验证器

export function checkIfEndDateAfterStartDate(
  c: AbstractControl
): { [key: string]: boolean } | null {
  const beginDateControl = c.get('beginDate');
  const endDateControl = c.get('endDate');

  //safety check
  if (beginDateControl?.pristine && endDateControl?.pristine) {
    return null;
  }

  **console.log(endDateControl?.value);** // this console logs the actual form control so on null checks it comes back as Valid. The form control object though in console log has the value as null

  if (
    (beginDateControl?.value && !endDateControl?.value) 
  ) {
//never hits because of above
    return { datesInvalid: true };
  }

  return null;
}

You have a mistake on the initialize form with the formBuilder您在使用 formBuilder 初始化表单时出错

Try to change to this way.尝试改变这种方式。

this.form = this.fb.group({
          reservationNumber: [],
          reservationName: []
          dateGroup: this.fb.group(
            {
              beginDate: [],
              endDate: [],
            },
            { validators: checkIfEndDateAfterStartDate }
          ),
          organizationName: []
          firstName:[]
          lastName:  []
       
        });
    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 角反应形式-获取用于自定义验证程序的formControl - Angular reactive forms - get formControl for custom validator 如何在FormControl Angular上设置自定义验证器 - How to Set Custom Validator on FormControl Angular FormControl在Angular 2中返回空值 - FormControl returning empty value in Angular 2 Angular:返回null的自定义验证器使Angular抛出错误 - Angular: Custom validator returning null makes Angular throw an error 返回{__zone_symbol__state:null,__ zone_symbol__value:Array(0)}的Angular自定义异步验证器 - Angular Custom Async Validator returning {__zone_symbol__state: null, __zone_symbol__value: Array(0)} Angular Form 自定义验证器 http observable 在 http 调用时返回 null - Angular Form custom validator http observable returning null on http call Angular FormControl 值设置为 null 内部服务 - Angular FormControl value set to null inside service Angular - 可以在没有初始值的情况下在 FormControl 上使用验证器吗? - Angular - Can you use a Validator on a FormControl without an initial value? Angular 自定义组件FormControl传值 - Angular custom component FormControl pass value 角2-自定义表单验证程序不起作用-无法读取null的属性“值” - Angular 2 - Custom Form Validator not working - Cannot read property 'value' of null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM