Having trouble with form validation in Angular 12

CSS

/* Add your custom CSS styles here */

HTML Markup

<div class="container-fluid">
  <h2>User Profile Form</h2>
  <form [formGroup]="registrationForm" (ngSubmit)="onSubmit()">

    {{registrationForm.value|json}}

    <div class="form-group">
      <label>Username</label>
      <input [class.is-invalid]="registrationForm.get('userName').invalid && registrationForm.get('userName').touched"
             formControlName="userName" type="text" class="form-control">
    </div>

    <div class="form-group">
      <label>Password</label>
      <input formControlName="password" type="password" class="form-control">
    </div>

    <div class="form-group">
      <label>Confirm Password</label>
      <input formControlName="confirmPassword" type="password" class="form-control">
    </div>

    <div formGroupName="address">
      <div class="form-group">
        <label>City</label>
        <input formControlName="city" type="text" class="form-control">
      </div>

      <div class="form-group">
        <label>State</label>
        <input formControlName="state" type="text" class="form-control">
      </div>

      <div class="form-group">
        <label>Postal Code</label>
        <input formControlName="postalCode" type="text" class="form-control">
      </div>
    </div>

    <button class="btn btn-primary" type="submit" [disabled]="!registrationForm.valid">Register</button>
    <button (click)="loadApiData()" class="btn btn-secondary" type="button">Load API Data</button>

  </form>

</div>

TypeScript File

import { Component, OnInit } from '@angular/core';
import {FormBuilder, FormControl, FormGroup, Validators} from "@angular/forms";

@Component({
  selector: 'app-user-profile-form',
  templateUrl: './user-profile-form.component.html',
  styleUrls: ['./user-profile-form.component.css']
})
export class UserProfileFormComponent implements OnInit {

  registrationForm: FormGroup;

  constructor(private fb: FormBuilder) {
    
    this.registrationForm = this.fb.group({
      userName: ['JohnDoe', Validators.required],
      password: [''],
      confirmPassword: [''],
      address:  this.fb.group({
        city: [''],
        state:[''],
        postalCode:['']
      })
    });

  }

  ngOnInit(): void {
  }

  onSubmit() {
    console.warn(this.registrationForm.value);
  }

  loadApiData(){
    this.registrationForm.setValue({
      userName: '',
      password: '12345',
      confirmPassword: '12345',
      address: {
        city: 'New York',
        state: 'NY',
        postalCode:'10001'
      }
    })
  }

}

Having trouble with validation in Angular 12? The error message reads:

Error: user-profile-form/user-profile-form.component.html:9:67 - error TS2531: Object is possibly 'null'.

9       <input [class.is-invalid]="registrationForm.get('userName').invalid && registrationForm.get('userName').touched"
                                                                ~~~~~~~

  user-profile-form/user-profile-form.component.ts:6:16
    6   templateUrl: './user-profile-form.component.html',

Error occurs in the template of component UserProfileFormComponent.


Error: user-profile-form/user-profile-form.component.html:9:111 - error TS2531: Object is possibly 'null'.

9       <input [class.is-invalid]="registrationForm.get('userName').invalid && registrationForm.get('userName').touched"
                                                                                                                ~~~~~~~

  user-profile-form/user-profile-form.component.ts:6:16
    6   templateUrl: './user-profile-form.component.html',
                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Error occurs in the template of component UserProfileFormComponent.

Looking for a fix? Check your code and ensure proper implementation of form validation rules.

Answer №1

To access the form controls in your Angular registration form, make sure to use registrationForm.controls.

In your HTML template:

<div class="container-fluid">
  <h2>Registration Form</h2>
  <form [formGroup]="registrationForm" (ngSubmit)="onSubmit()">

    {{registrationForm.value|json}}

    <div class="form-group">
      <label>Username</label>
     
      <input [class.is-invalid]="registrationForm.controls.userName.invalid && registrationForm.controls.userName.touched"
             formControlName="userName" type="text" class="form-control">
    </div>

    <div class="form-group">
      <label>Password</label>
      <input formControlName="password" type="password" class="form-control">
    </div>

    <div class="form-group">
      <label>Confirm Password</label>
      <input  formControlName="confirmPassword" type="password" class="form-control">
    </div>

    <div formGroupName="address">
      <div class="form-group">
        <label>City</label>
        <input  formControlName="city" type="text" class="form-control">
      </div>

      <div class="form-group">
        <label>State</label>
        <input  formControlName="state" type="text" class="form-control">
      </div>

      <div class="form-group">
        <label>Postal Code</label>
        <input  formControlName="postalCode" type="text" class="form-control">
      </div>
    </div>

    <button class="btn btn-primary" type="submit" [disabled]="!registrationForm.valid">Register</button>
    <button (click)="loadApiData()" class="btn btn-secondary" type="button">Load API Data</button>

  </form>

</div>

In the TypeScript file:

import { Component, OnInit } from '@angular/core';
import {FormBuilder, FormControl, FormGroup, Validators} from "@angular/forms";

@Component({
  selector: 'app-product-form2',
  templateUrl: './product-form2.component.html',
  styleUrls: ['./product-form2.component.css']
})
export class ProductForm2Component implements OnInit {

  registrationForm: FormGroup;

  constructor(private fb: FormBuilder) {

    this.registrationForm = this.fb.group({
      userName: ['Devesh', Validators.required],
      password: [''],
      confirmPassword: [''],
      address:  this.fb.group({
        city: [''],
        state:[''],
        postalCode:['']
      })
    });

  }

  ngOnInit(): void {
  }

  onSubmit() {
    // TODO: Use EventEmitter with form value
    console.warn(this.registrationForm.value);
  }

  loadApiData(){
    this.registrationForm.setValue({
      userName: '',
      password: '123',
      confirmPassword: '123',
      address: {
        city: '123',
        state: '123',
        postalCode:'123'
      }
    })
  }

}

Check out a demo of the code at this StackBlitz link

Answer №2

<div class="container-fluid">
  <h2>Sign-Up Form</h2>
  {{signUpForm.value | json}}
  <form [formGroup]="signUpForm">
    <div class="form-group">
      <label>Username</label>
      <input [class.is-invalid]="signUpForm.get('userName')?.invalid && signUpForm.get('userName')?.touched " formControlName="userName" type="text" class="form-control">
    </div>
    <div class="form-group">
      <label>Password</label>
      <input formControlName="password" type="password" class="form-control">
    </div>
    <div class="form-group">
      <label>Confirm Password</label>
      <input formControlName="confirmPassword" type="password" class="form-control">
    </div>
    <div formGroupName="address">
      <div class="form-group">
        <label>City</label>
        <input formControlName="city" type="text" class="form-control">
      </div>

      <div class="form-group">
        <label>State</label>
        <input formControlName="state" type="text" class="form-control">
      </div>

      <div class="form-group">
        <label>Postal Code</label>
        <input formControlName="postalCode" type="text" class="form-control">
      </div>
    </div>
    <br/>
    <button class="btn btn-primary" type="submit">Create Account</button>
    <button (click)="fetchInformation()" class="btn btn-primary ml-5" type="button">Load User Data</button>
  </form>
</div>

Answer №3

It seems like the registrationForm object may not be initialized at that specific moment.

You could consider adding a null check condition for the registrationForm as well.

<input [class.is-invalid]="**registrationForm** && registrationForm.get('userName').invalid && registrationForm.get('userName').touched"
/>

Alternatively, you can enclose your entire form within an if container

<ng-container *ngIf="registrationForm">
<!-- your form -->
</ng-container>

Answer №4

When working with Angular, you can refer to this guide for validating input in reactive forms.

To define getters for the properties, you can use the following syntax:

get name() { return this.heroForm.get('name'); }

get power() { return this.heroForm.get('power'); }

After defining the getters, update your HTML code as follows:

[class.is-invalid]="userName.invalid && userName.touched"

This adjustment will help eliminate any 'is possibly null' errors that may occur.

Alternatively, you can add a helper function to your .ts file like this:

  get f() {
    return this.issueForm.controls;
  }

Then modify your HTML code by referencing the helper function:

[class.is-invalid]="this.f.userName.invalid && this.f.userName.touched"

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

Customize the height of individual carousel items in Primeng carousel

Hey there, I'm currently using the primeng carousel component and running into an issue where the carousel height is always based on the tallest item. I am looking to have an automatic height for each individual carousel item instead. Do you think th ...

How to seamlessly incorporate Polymer Web Components into a Typescript-based React application?

Struggling to implement a Polymer Web Components tooltip feature into a React App coded in TypeScript. Encountering an error during compilation: Error: Property 'paper-tooltip' does not exist on type 'JSX.IntrinsicElements' To resolve ...

Creating a custom type for accepted arguments in a Typescript method

Below is the structure of a method I have: const a = function (...args: any[]) { console.log(args); } In this function, the type of args is any[]. I am looking to create a specific type for the array of arguments accepted by this method in Typescript. ...

What's the best way to group rows in an angular mat-table?

I am working on a detailed mat-table with expanded rows and trying to group the rows based on Execution Date. While looking at this Stackblitz example where the data is grouped alphabetically, I am struggling to understand where to place the group header c ...

What is the best approach for retrieving a User from my Angular front-end service?

INQUIRY: I'm attempting to retrieve the details of the currently logged in user in my Angular 2 frontend service with the following code: UserModel.findById(userID, function (err, user) { It appears that this behavior is achievable from the browse ...

The Angular Material Nav Sidebar is specifically designed to appear only when resizing the screen to

I am currently following a tutorial on setting up Angular Material Sidenav with a toolbar from this video (https://www.youtube.com/watch?v=Q6qhzG7mObU). However, I am encountering an issue where the layout only takes effect after resizing the page. I am no ...

Hold off until the RxJS dispatch is resolved

I am working on integrating a "next step" feature into my Angular 6 webapp. When the user clicks the "next step" button, the frontend triggers an action to update the database with the data in the store, another action to retrieve processed data from a Spr ...

Guide on accessing mobile information and sim card details with Ionic 3 and Cordova on Android devices

Just started using Ionic and I'm looking for guidance on how to retrieve mobile and sim details with Ionic 3 and Cordova for Android. Any help is greatly appreciated in advance! ...

Using electron cookies in Angular involves integrating Electron's native cookie functionality into an

I am currently dealing with electron and looking to implement cookies conditionally in my project. If the application is built using electron, I want to utilize Electron Cookies; otherwise, I plan to use Angular Cookies. However, I'm encountering diff ...

Defining ReactNode as a prop in a TypeScript React component: A comprehensive guide

Is there a way to create a React component in TypeScript that accepts another React component as a prop? I am attempting to write the following code: const MyComponent = () => ( <span>Hello</span> ); // when trying this, I get an error m ...

Develop a variety of Jabber client echo bots

Hello Stackoverflow Community, I've been trying different approaches to resolve my issue, but I keep ending up with stack overflow errors. Programming Language: Typescript Main Objective: To create multiple instances of the Client Class that can be ...

Display a message stating "No data available" using HighCharts Angular when the data series is empty

My Angular app utilizes Highchart for data visualization. One of the requirements is to display a message within the Highchart if the API returns an empty data set. I attempted a solution, but unfortunately, the message does not appear in the Highchart a ...

Angular 2 radio button problem with model-driven form

I'm facing a challenge with getting radio buttons to function correctly in my reactive form. Below is the code for my component: export class SettingsFormComponent implements OnInit { public sForm: FormGroup; public submitted: boolean; d ...

Troubleshooting the TS2559 error when using radium in React typescript: CSSProperties type mismatch

Looking to integrate Typescript into React and incorporate some styling using Radium. I understand that JSX style does not support media queries, but I'm unsure how to resolve this issue. Can anyone provide guidance? Thank you! Encountering errors w ...

"Techniques for extracting both the previous and current selections from a dropdown menu in Angular 2

How can I retrieve the previous value of a dropdown before selection using the OnChange event? <select class="form-control selectpicker selector" name="selectedQuestion1" [ngModel]="selectedQuestion1" (Onchange)="filterSecurityQuestions($event.t ...

Issue encountered while executing npm install due to rxjs version discrepancy

While developing a new Angular App, I utilized Angular CLI by running ng new. However, when attempting to launch the app with npm start, I encountered the following error: ERROR in node_modules/rxjs/internal/types.d.ts(81,44): error TS1005: ';' ...

Having trouble with data types error in TypeScript while using Next.js?

I am encountering an issue with identifying the data type of the URL that I will be fetching from a REST API. To address this, I have developed a custom hook for usability in my project where I am using Next.js along with TypeScript. Below is the code sni ...

Running into an issue with data retrieval in Angular from the backend layer

Greetings! I recently ran into a minor issue while working on an Angular application. My goal was to retrieve a list of "Menu" elements from the backend and use them in my application. The problem arose when I used the get method to fetch the data. It see ...

How can one trigger a service method in nestjs through a command?

I am looking to run a service method without relying on API REST - I need to be able to execute it with just one command ...

Webpack is failing to recognize certain CSS files

My development stack includes Vue.js 2.5.15, Webpack 4.12.0, css-loader 0.28.11, ASP.Net Core 2.1 in Visual Studio 2017. Starting with the Visual Studio asp.net core template project for Vue and Typescript, I prefer to have individual small CSS files with ...