Error: Unable to access the 'registerControl' property of the object due to a type mismatch

I'm struggling to set up new password and confirm password validation in Angular 4. As a novice in Angular, I've attempted various approaches but keep encountering the same error. Seeking guidance on where my mistake lies. Any help in resolving this issue would be greatly appreciated to ensure my code functions properly.

password.ts

    import { Component, Injectable } from '@angular/core';
    import { IonicPage, NavController, NavParams, PopoverController } from 'ionic-angular';
    import { Http, Headers } from '@angular/http';
    import 'rxjs/add/operator/map';
    import { RequestOptions } from '@angular/http';
    import 'rxjs/operator/delay';
    import 'rxjs/operator/mergeMap';
    import 'rxjs/operator/switchMap';
    import { NgForm } from '@angular/forms';
    import { FormsModule, ReactiveFormsModule } from '@angular/forms';
    import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';

    @IonicPage()
    @Component({
        selector: 'page-password',
        templateUrl: 'password.html',
    })
    export class PasswordPage {

        constructor(public navCtrl: NavController, public navParams: NavParams, private http: Http, public popoverCtrl: PopoverController) {
        }

        ionViewDidLoad() {
            console.log('ionViewDidLoad PasswordPage');
        }

        public Credentials: FormGroup;

        ngOnInit() {
            this.Credentials = new FormGroup({});
            this.Credentials.addControl('Password', new FormControl('', [Validators.required]));
            this.Credentials.addControl('Confirmation', new FormControl('', [Validators.compose([Validators.required, this.validateAreEqual.bind(this)])]));
        }

        private validateAreEqual(fieldControl: FormControl) {
            return fieldControl.value === this.Credentials.get("Password").value ? null : { NotEqual: true };
        }

password.html

    <ion-content padding>
      <div class="loginContainer">
        <div class="centerLogo">
          <h1>
            <a href="#">
              <img src="images/logomaster.jpg" alt="" />
            </a>
          </h1>
        </div>
        <form #fm="ngForm" (ngSubmit)="test(fm)" novalidate>
          <ul id="tabs">

            <li (click)="showemail=false">
              <a id="tab2">Set your PIN</a>
            </li>

          </ul>
          <div class="form-group row" formGroupName="passwords" id="tab2C">
            <div class="form-group">
              <ion-item>
                <ion-input type="password" class="form-control" formControlName="password" title="Please enter your password"></ion-input>
              </ion-item>
            </div>
            <div class="form-group">
              <ion-item>
                <ion-input type="password" class="form-control" formControlName="confirmedPassword" title="Please re-enter your password"></ion-input>
              </ion-item>
            </div>
            <div class="btnRow">
              <input class="loginBtn" type="submit" id="phoneBtn" value="Continue" />
            </div>
          </div>
        </form>
      </div>
    </ion-content>

Answer №1

There are some issues in your template that need to be addressed. First, you have not correctly marked your formgroup in the template. Additionally, you are declaring a formGroupName named passwords, which does not exist in your form.

Your form should be structured like this:

<form (ngSubmit)="test(fm)" [formGroup]="Credentials">
  <ul id="tabs">
    <li (click)="showemail=false">
      <a id="tab2">Set your PIN</a>
    </li>
  </ul>
  <div class="form-group row" id="tab2C">
    <div class="form-group">
      <ion-item>
        <ion-input type="password"formControlName="Password"></ion-input>
      </ion-item>
    </div>
    <div class="form-group">
      <ion-item>
        <ion-input type="password"formControlName="Confirmation"></ion-input>
      </ion-item>
      <ion-item *ngIf="Credentials.hasError('NotEqual', 'Confirmation')">
        NOT SAME
      </ion-item>
    </div>
    <div class="btnRow">
      <input class="loginBtn" type="submit" id="phoneBtn" value="Continue" />
    </div>
  </div>
</form>

DEMO

It's important to note that the validation message will not display when editing the Password field after matching it with the confirm field. To make this work bidirectionally, consider implementing a custom validator:

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Exploring the most effective strategies for creating a brand-new type in TypeScript?

In the execution environment I'm working with, there are several global constants that represent different directions: TOP = 1 TOP_RIGHT = 2 RIGHT = 3 BOTTOM_RIGHT = 4 BOTTOM = 5 BOTTOM_LEFT = 6 LEFT = 7 TOP_LEFT = 8 These constants are not just ran ...

On which platform is the getFeatureInfo request constructed using Cesium?

Currently, I am working with Cesium and Angular. I am trying to locate where the request URL is generated for GetFeatureInfo in Cesium, but unfortunately I am unable to find it. My goal is to display feature information when clicking on the map. However, ...

Minimize the count of switch cases that are not empty

How can I optimize the number of cases in my switch statement to align with SonarQube recommendations? Currently, I have 37 cases in a switch statement, but SonarQube recommends only 30. I believe that my code is functioning correctly, and the issue lies ...

Preventing angular router events from being logged in the console

In the process of developing an angular application, I have encountered a challenge with the {RouterModule} from @angular/router. Although I rely on console.log to troubleshoot my app, the router module has unleashed a flurry of router events in the conso ...

What is the correct way to include a new property in the MUI Link component using TypeScript?

Currently, within my mui-theme.ts configuration file, I have the following setup: const theme = createTheme(globalTheme, { components: { MuiLink: { variants: [ { props: { hover: 'lightup' }, style: { ...

Transform a JSON array into an array of objects using typescript

I have a JSON array that I need to convert into an object type array JSON array [ 0:{code: "00125", scheme: "0001", plotNumber: "125", propType: "001", plotType: "001"} 1:{code: "190", scheme: "0001", plotNumber: "NA 190", propType: "001", plotType: "0 ...

The Typescript compiler is unable to locate the module './lib'

I'm currently integrating the winston-aws-cloudwatch library into my TypeScript server-side application. If you want to replicate the issue, I have provided a SSCCE setup on GitHub. Here are the details: index.ts import logger from './logger& ...

Utilize the fetch function within a React functional component

I have been experimenting with different methods to fetch data only once before rendering, but I am encountering some challenges: It is not possible to call dispatch in componentDidMount as there is a restriction that it can only be done in Functional c ...

Upgrade Angular to the most recent version available

Our organization is looking to upgrade our existing Angular version from v5 to either the latest version 12 or 13. After conducting some research, I believe that transitioning directly from 5 to 12-13 may be too significant. What would be the most effect ...

Maximizing Jest's potential with multiple presets in a single configuration file/setup

Currently, the project I am working on has Jest configured and testing is functioning correctly. Here is a glimpse of the existing jest.config.js file; const ignores = [...]; const coverageIgnores = [...]; module.exports = { roots: ['<rootDir&g ...

How can you set a value to a radio button with Angular 4?

How can I set the JSON value for the radio buttons (Unlimited, Custom) below? I tried using [(ngModel)]="user.accessSchedule.fullAccess", but it's not working. <ol-radio-group [selected]="unlimitedAccessSchedule" id="accessSchedule" [(ngModel)]=" ...

Guide on how to automatically navigate to the homepage upon logging in using Angular

It's puzzling to see my navbar appearing on the login page as well. Another issue is that even after successfully logging in and receiving the token, I am not redirected to the homepage. Let me outline what I have tried so far - starting with my modul ...

Any idea how to resolve this typescript typing issue: "The argument, either a string or number, cannot be assigned to the parameter of type 'SetStateAction<string>'"?

Just recently delving into TypeScript, I've encountered a persistent typing problem that has proven challenging to resolve despite numerous attempts. The error message causing me trouble reads as follows: Argument of type 'string | number' ...

Transforming a TypeScript enum into an array of objects

My enum is defined in this structure: export enum GoalProgressMeasurements { Percentage = 1, Numeric_Target = 2, Completed_Tasks = 3, Average_Milestone_Progress = 4, Not_Measured = 5 } However, I want to transform it into an object ar ...

Pending activation of the Timer Fired event

Below is some code I have for implementing swipe gesture functionality: this.topSlide = this.elementRef.nativeElement.querySelector('.product_rate_slide'); if (this.topSlide) { this.topSlide.addEventListener('touchstart', this.hand ...

Having trouble retrieving information from hash fetch fragment following authentication redirection in Angular 4

Once the authorization process is complete, the browser will be redirected to the following URL: &token_type=bearer&state=XYZ&expires_in=3599 However, just before I can retrieve the details, everything seems to disappear and the browser' ...

Disable Styling and Override Readonly CSS restrictions

What is the correct approach for customizing material design styling when input fields are disabled? Out of the Box Explore Angular Material Input Examples I managed to achieve my objective using the following CSS, but I am concerned if this is a recomm ...

Here is a way to retrieve the name of a ref object stored in an array using Vue.js 3 and Typescript

I have a Form, with various fields that I want to get the value of using v-model and assign them to ref objects. In order to populate my FormData object with this data, I require both the name and the value of the ref objects. Unfortunately, I am struggli ...

The Angular ngFor directive is failing to loop through the provided

This is the format I created <div *ngIf="attachments"> <div> {{ 'attachments' | translate }} </div> {{ attachments.data[0].title }} <!-- this line works fine --> <div *ngFor="let item of attachm ...

Tips for saving data from an observable into a variable or property

I've been diligently working on a project utilizing both Angular and Firebase/Firestore. Here is the code snippet that I have implemented: this.cadet = this.cadetCollection.valueChanges().subscribe(x => { this.cadetName = x[0].Cadet; this.cad ...