Search code examples
javascriptangulartypescriptangular-reactive-formsangular-forms

Undefined collision in Angular 8


I am using Angular 8 with "typescript": "~3.5.3". Trying to do the undefined collision

const { testLocation } = this.ngr.getState();
    this.step2 = testLocation && testLocation.step2 ? testLocation.step2 : {};
    this.$form = this.fb.group({
      // tslint:disable-next-line: max-line-length
      email: [this.step2.email || '', [Validators.required, Validators.email, Validators.pattern('^[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,4}$')]],
      phone: [this.step2.phone || ''],
      configuration: this.fb.group({
        idTypesAccepted: this.buildIdentityDocumentTypeFormArr(this.identityDocumentType),

        sessionOpenDays: [this.step2.configuration.sessionOpenDays || '', Validators.required],
        sessionOpenHours: [this.step2.configuration.sessionOpenHours || '', Validators.required],
        sessionOpenMinutes: [this.step2.configuration.sessionOpenMinutes || '', Validators.required],

        sessionCloseDays: [this.step2.configuration.sessionCloseDays || '', Validators.required],
      sessionCloseHours: [this.step2.configuration.sessionCloseHours || '', Validators.required],
        sessionCloseMinutes: [this.step2.configuration.sessionCloseMinutes || '', Validators.required],

        applicationExpiryDays: [this.step2.configuration.applicationExpiryDays || '', Validators.required],
        applicationExpiryHours: [this.step2.configuration.applicationExpiryHours || '', Validators.required],
        applicationExpiryMinutes: [this.step2.configuration.applicationExpiryMinutes || '', Validators.required],

        paymentCloseDays: [this.step2.configuration.paymentCloseDays || '', Validators.required],
        paymentCloseHours: [this.step2.configuration.paymentCloseHours || '', Validators.required],
        paymentCloseMinutes: [this.step2.configuration.paymentCloseMinutes || '', Validators.required]
      })
    });

In the above code this.step2.configuration, configuration is undefined. How can I surpass this. I need to fill up either empty value or value from then this.step2.configuration


Solution

  • The only three ways I see here is either: Check for undefined at start and override with dummy model

    this.step2 = testLocation && testLocation.step2 ? testLocation.step2 : {};
    if(!this.step2.configuration) {
      this.step2.configuration = new Configuration(); // filling the blanks with a dummy data model which has nulled or '' variables;
    }
    

    alternatively duplicate the entire formbuilding

    if(!this.step2.configuration) {
      this.fb.group({
        sessionOpenDays: ['', Validators.required],
        [...]
      });
      return;
    }
    this.fb.group({[...]});
    

    or you could switch to Angular 10 (not sure which version of 9 introduced this, but it certainly is available on 10)

    this.step2 = testLocation && testLocation.step2 ? testLocation.step2: {};
    this.fb.group({
      sessionOpenDays: [this.step2.configuration?.sessionOpenDays || '', Validators.required],
    [...]
    });
    

    in angular 10, just like it (almost) always was in the html-templates, a questionmark checks if the property is availabe and if not, initializes the formControl with a null value. Be aware though that a direct call to the form field would also yield null instead of '' then.