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

Can HTML variables be accessed in lines of code before they are declared in HTML?

In line 1 of my code, I am trying to access the rowData variable which is declared in the second HTML line. However, I keep getting the error message "Property 'rowData' does not exist on type 'AppComponent'" for that line. Strangely, t ...

Tips for resolving Typescript type error when overriding MuiContainer classes

My application is divided into two main files: (https://codesandbox.io/s/react-ts-muicontainer-override-yywh2) //index.tsx import * as React from "react"; import { render } from "react-dom"; import { MuiThemeProvider } from "@material-ui/core/styles"; imp ...

What is the best way to integrate Tawk.to into a React application while using typescript?

Having some issues integrating tawk.to into my website built with React and TypeScript. I have installed their official npm package, but encountered an error message: import TawkMessengerReact from '@tawk.to/tawk-messenger-react'; Could not fin ...

Testing the unit with a customized header in the interceptor

I've been struggling to execute a unit test within an Angular 6 interceptor, but despite numerous attempts, I keep encountering the following error: Error: Expected one matching request for criteria "Match by function: ", found none. I'm rela ...

What is the best way to include text or a label on my Angular map without using a marker?

I am currently using the agm-map module in my angular project. I want to place a label or text at the center of a polygon on the map without using markers. How can I achieve this functionality in Typescript? I attempted to use the MapLabel Utility for thi ...

When using RxJS forkjoin, it will cease subscription if there is a flatMap present within one of the observables it is awaiting

I have been experimenting with nested calls using rxjs and trying to implement nested forkJoins. However, I have encountered an issue where the outer forkJoin stops returning a result when there is a flatMap inside the inner observable. Here is a snippet ...

Infinite scrolling pagination feature in ag-grid not functioning properly with Angular

Upon the initial component load, all data appears as expected. However, when switching to the next page, the grid clears and an error is shown in the console: ERROR Error: ag-Grid: unable to draw rows while already drawing rows. It seems that a grid API ...

Utilizing Angular 7, Ngrx, and Rxjs 6 to efficiently share state data among lazily loaded modules

Currently, I am working with Angular 7 alongside Ngrx and Rxjs 6. In my project, I have two lazy loaded modules named A and B, each with its own selectors and reducers. The challenge I am facing is accessing the data stored in module B's state from m ...

Generics in Typescript implemented for a React component that accepts an array of records along with an array of

I am currently working on developing a straightforward typed react component designed to display a table from an array of objects. The input data is structured as follows: // array of records containing data to render in the table data = [ { one: 1, ...

The type of 'data' is assumed to be 'any[]' without being explicitly stated

I am encountering several type errors in the function below, and as a newcomer to Typescript, I'm unsure about how to fix them. private fetchFromUrl = () => { var data = [] fetch(`${process.env.PUBLIC_URL}/tempData/monthly.csv`) .t ...

What could be causing the getTotals() method to malfunction?

I have been working on a finance app that is designed to update the "income", "expenses", and "balance" tables at the top each time a new item is added by the user. However, the current code seems to be failing in updating these values correctly based on u ...

Guide to showcasing multiple paths of Firebase data on a single Angular page

I am working with a basic database structure that includes information about groups, events, and users. Here is an example: { "groups": { "123": { "name": "developers", "users": { "1": true }, "users_count": 1 } ...

Tips for ensuring a function in Angular is only executed after the final keystroke

I'm faced with the following input: <input type="text" placeholder="Search for new results" (input)="constructNewGrid($event)" (keydown.backslash)="constructNewGrid($event)"> and this function: construct ...

Troubleshooting why @HostListener seems to be malfunctioning

I implemented a drag event listener using the HostListener to determine if an element is being dragged inside or outside of the browser window. When the element is dragged inside the window, a "drop zone" div displays the message "Drop your files here". Ho ...

Retrieve JSON information from an API using Angular 2

I am facing an issue with retrieving JSON data from an API that I created. Despite using Angular code to fetch the data, it seems to be unsuccessful. Here is the code snippet: getBook(id: string){ return this._http.get(this.url + 'books/' + ...

How to stop a checkbox from being selected in Angular 2

I have a table with checkboxes in each row. The table header contains a Check All checkbox that can toggle all the checkboxes in the table rows. I want to implement a feature where, if the number of checkboxes exceeds a certain limit, an error message is ...

The NgbTypeahead element is not able to scroll when placed within a scrollable container

Currently, I am utilizing the NgbTypeahead component from ng-bootstrap. The issue I am facing is that when I place the typeahead component within a scrollable element and proceed to scroll down, the position of the dropdown container remains unchanged. &l ...

Angular 8 encountering issues with incomplete/impartial JSON parsing

Upon receiving a JSON object through a Socketio emit, the data appears as follows: { "single":[ { "name":"Xavi555", "data":[ { "_id":"5e2ea609f8c83e5435ebb6e5", "id":"test1", "author":"someone", ...

Accessing information from req.files in a TypeScript environment

I am currently using multer for uploading images. How can I retrieve a specific file from req.files? Trying to access it by index or fieldname has been unsuccessful. Even when I log it, I see that it's a normal array, so I suspect the issue may be rel ...

Choose FormGroup using Angular Directive

Can Angular reactive form controls be selected for a custom directive in a different way? Take a look at this code snippet: @Directive({ selector: '[formControl], [formControlName]', }) export class MyDirective { constructor( priv ...