How to get age from this form group’s function in Angular and store it on back-end?

I’ve found interesting function here from another post which checks if the person is 18 years or older or not. I want to get age converted and set in the back-end separately but the point is that I don’t have age input in HTML. Here is the code:

this.SignupForm = new FormGroup({
        'username': new FormControl(null, [Validators.required
        ]),
       'email': new FormControl(null, [Validators.required, Validators.email], this.forbiddenEmails),
       'password': new FormControl(null, [Validators.required,Validators.minLength(4)
        ]),
         'day': new FormControl(null, [Validators.required, Validators.maxLength(2),
         ]),
         'month': new FormControl(null, [Validators.required, Validators.maxLength(2)
         ]),
         'year': new FormControl('', [Validators.required, Validators.maxLength(4), Validators.minLength(4)
         ]),
     });
     this.SignupForm.setValidators(this.minimumAge(18));
   }
      private minimumAge(age: number): ValidatorFn {
        return (fg: FormGroup): ValidationErrors => {
          let result: ValidationErrors = null;
          if (fg.get('year').valid && fg.get('month').valid && fg.get('day').valid) {
            // carefull, moment months range is from 0 to 11
            const value: { year: string, month: string, day: string } = fg.value;
            const date = moment({ year: +value.year, month: (+value.month) - 1, day: +value.day }).startOf('day');
            if (date.isValid()) {
              // https://momentjs.com/docs/#/displaying/difference/
              const now = moment().startOf('day');
              const yearsDiff = date.diff(now, 'years');
              if (yearsDiff > -age) {
                result = {
                  'minimumAge': {
                    'requiredAge': age,
                    'actualAge': yearsDiff
                  }
                };
              }
            }
          }
          return result;
        }
      }

...

signupUser() {
  this.authService.registerUser(this.SignupForm.value).subscribe(
    data => {
      this.tokenService.SetToken(data.token);
      this.SignupForm.reset();
      setTimeout(() => {
        this.router.navigate(['posts']);
      }, 3000);
    },
    err => {
      if (err.error.msg) {
        this.errorMessage = err.error.msg[0].message;
      }

      if (err.error.message) {
        this.errorMessage = err.error.message;
      }
    }
  );
}

How can I get converted age value from this typescript function and send it to the database without using age input in HTML?

Source: Angular

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.