Categories

Saturday, December 21, 2024
#919814419350 therichposts@gmail.com
AngularAngular 17

Angular 17 Reactive Form with Dynamic Fields & values

Angular 17 Reactive Form with Dynamic Fields & values

Angular 17 Reactive Form with Dynamic Fields & values, angular forms, angular form validation.
Hello friends, welcome back to my blog. Today in this blog posts, I am going to tell you, Angular 17 Reactive Form with Dynamic Fields & values.

Angular Reactive Form

Post Working:

In this post, I am doing, Angular reactive form with dynamic fields, Angular reactive form with dynamic input values, Angular reactive form with dynamic fields labels and Angular reactive form with dynamic fields validations.

Angular 17 came and if you are new then you must check below link:

  1. Angular17 Basic Tutorials

Friends now I proceed onwards and here is the working code snippet for and please use carefully this to avoid the mistakes:

1. Firstly friends we need fresh angular 17 setup and for this we need to run below commands but if you already have angular 17 setup then you can avoid below commands. Secondly we should also have latest node version installed on our system:

npm install -g @angular/cli 

ng new angularreactiveform //Create new Angular Project

cd angularreactiveform // Go inside the Angular Project Folder

ng serve --open // Run and Open the Angular Project

http://localhost:4200/ // Working Angular Project Url

2. Now friends, here we need to run below commands into our project terminal to install bootstrap(for good looks) module into our angular application:

npm install bootstrap --save

3. After done with commands add below code into your angular.json(file will be in project root folder) file:

...
"styles": [
              ...
              
              "node_modules/bootstrap/dist/css/bootstrap.min.css",
            ]
...

4. Friends now we need to run again below command to run the project:

ng serve

5. Now friends we need to  add below code into your angularreactiveform/src/app/app.component.ts file:

import { Component, ViewChild, ElementRef  } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterOutlet } from '@angular/router';
import { FormBuilder, FormGroup, Validators, FormControl, ReactiveFormsModule } from '@angular/forms';
@Component({
  selector: 'app-root',
  standalone: true,
  imports: [CommonModule, RouterOutlet, ReactiveFormsModule],
  templateUrl: './app.component.html',
  styleUrl: './app.component.css'
})
export class AppComponent {
  title = 'angular forms';
 
  //Reactive form custom json data
  fields=[{id:'firstname',label:'First Name',value:'Ajay'},{id:'lastname',label:'Last Name',value:'Malhotra'}];
  form=new FormGroup({})
  
  constructor(private formBuilder: FormBuilder){}
  onSubmit() {
    
   
    // stop here if form is invalid
    if (this.form.invalid) {
        return;
    }
    else
    {
      
      alert("WOW!! You have been done with reactive form.");
    }
   
  }
  
  
    ngOnInit() {
      //form validations
   
      this.fields.forEach(x=>{
        this.form.addControl(x.id,new FormControl(x.value,[Validators.required]))
       })
    
    }
}

7. Now friends we need to  add below code into your angularreactiveform/src/app/app.component.html file:

<div class="container p-5">
    <h1>Angular 17 Reactive Form with Dynamic Fields & values. therichpost.com</h1>
    
    <form [formGroup]="form" (ngSubmit)="onSubmit()">

        <div *ngFor="let control of form.controls |keyvalue;let i=index" class="form-group">
        <label>{{fields[i].label}}</label>
        <input class="form-control" [formControl]="control.value">
            
        </div>

        <button [disabled]="form.invalid" type="submit" class="btn btn-primary">Submit</button>
    </form>
</div>

Now we are done friends and if you have any kind of query then please do comment below.

Note: Friends, I just tell the basic setup and things, you can change the code according to your requirements. For better understanding must watch video above.

I will appreciate that if you will tell your views for this post. Nothing matters if your views will be good or bad.

Jassa

Thanks

therichpost
the authortherichpost
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 19, MedusaJs, Next.js, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.

Leave a Reply

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