Hi
I am getting error - Parse error Missing expected ]
Code
First Name
This is requiredThanks
Hi
I am getting error - Parse error Missing expected ]
First Name
This is requiredThanks
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jithu ThomasRank #437 · 5.7K reputationPosted Jun 5, 2024, 2:36 AM EDT
The error "Object is possibly null" occurs because TypeScript is indicating that
empForm.get('firstName').errorsmight benull. To address this, you can use the optional chaining operator (?.) combined with a nullish coalescing operator (??) to safely handle potentialnullvalues.Updated Html Templete: -
First Name This is required Minimum length is 2 Last Name This is required Email This is required Invalid email address SubmitComponent TypeScript for Reference:
import { Component } from '@angular/core'; import { FormBuilder, FormGroup, Validators } from '@angular/forms'; @Component({ selector: 'app-your-component', templateUrl: './your-component.component.html', styleUrls: ['./your-component.component.css'] }) export class YourComponent { empForm: FormGroup; constructor(private fb: FormBuilder) { this.empForm = this.fb.group({ firstName: ['', [Validators.required, Validators.minLength(2)]], lastName: ['', Validators.required], email: ['', [Validators.required, Validators.email]] }); } onSubmit() { if (this.empForm.valid) { console.log('Form Submitted', this.empForm.value); } else { console.log('Form is invalid'); } } }Ramco RamcoRank #462 · 5.4K reputationPosted Jun 5, 2024, 2:02 AM EDT
Hi
In place of ngIf can't we use @if
Thanks
Ramco RamcoRank #462 · 5.4K reputationPosted Jun 5, 2024, 2:16 AM EDT
Hi all
I want validation in Reactive forms
Thanks
Ramco RamcoRank #462 · 5.4K reputationPosted Jun 5, 2024, 2:30 AM EDT
Hi
On word errors it is giving error Object is possibly null
Thanks
Jithu ThomasRank #437 · 5.7K reputationPosted Jun 5, 2024, 1:55 AM EDT
The error you're encountering is due to incorrect syntax in the Angular template. The
*ngIfdirective should be used instead of@if, and the syntax for accessing form control errors is also incorrect.Here’s the corrected version of your template:
Explanation:
*ngIf: Angular templates use*ngIffor conditional rendering.getmethod on the form (empForm.get('firstName')).errors?.['required']to check for specific validation errors.Make sure that your component has the form group and form control defined correctly:
import { Component } from '@angular/core'; import { FormBuilder, FormGroup, Validators } from '@angular/forms'; @Component({ selector: 'app-your-component', templateUrl: './your-component.component.html', styleUrls: ['./your-component.component.css'] }) export class YourComponent { empForm: FormGroup; constructor(private fb: FormBuilder) { this.empForm = this.fb.group({ firstName: ['', Validators.required] }); } }With these changes, your form should work correctly, and the error messages should display appropriately based on the validation state.
Jithu ThomasRank #437 · 5.7K reputationPosted Jun 5, 2024, 2:05 AM EDT
In Angular templates, you cannot use
@ifbecause@ifis not a valid Angular directive. The correct directive to use for conditional rendering in Angular templates is*ngIf.example implimentation:-
Jithu ThomasRank #437 · 5.7K reputationPosted Jun 5, 2024, 2:26 AM EDT
You can able to use Angular's built-in validation capabilities. here is an implementation
app.module.ts
import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { ReactiveFormsModule } from '@angular/forms'; import { AppComponent } from './app.component'; @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, ReactiveFormsModule ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }Create a component that will contain your form.
your-component.component.ts
import { Component } from '@angular/core'; import { FormBuilder, FormGroup, Validators } from '@angular/forms'; @Component({ selector: 'app-your-component', templateUrl: './your-component.component.html', styleUrls: ['./your-component.component.css'] }) export class YourComponent { empForm: FormGroup; constructor(private fb: FormBuilder) { this.empForm = this.fb.group({ firstName: ['', [Validators.required, Validators.minLength(2)]], lastName: ['', Validators.required], email: ['', [Validators.required, Validators.email]] }); } onSubmit() { if (this.empForm.valid) { console.log('Form Submitted', this.empForm.value); } else { console.log('Form is invalid'); } } }Create the HTML template for your component to include form validation messages.
your-component.component.html
First Name This is required Minimum length is 2 Last Name This is required Email This is required Invalid email address SubmitNote this is a simple example for form validation in Angular using Reactive Forms