0
votes

I'm using reactive forms in CustomerAdd.html.

<form [formGroup]="CustomerProfileModel.FormCommonGroup">
        <div class="col-12 tab-content">
            <div class="row">
              <div class="col-md-10">
                <div class="form-group row m-b-10 m-t-20">
                  <label class="col-2 text-md-right col-form-label">Legal Name *</label>
                  <div class="col-4">
                    <input type="text" name="CommonLegalNameControl" required minlength="4" placeholder="" class="form-control" formControlName="CommonLegalNameControl" [(ngModel)]="CustomerProfileModel.LegalName">
                    <div *ngIf="CommonLegalNameControl?.invalid && (CommonLegalNameControl?.dirty || CommonLegalNameControl?.touched)" class="alert alert-danger">
                      <div *ngIf="CommonLegalNameControl?.errors.required">
                        Name is required.
                      </div>
                      <div *ngIf="CommonLegalNameControl?.errors.minlength">
                        Name must be at least 4 characters long.
                      </div>
                      <div *ngIf="CommonLegalNameControl.errors.forbiddenName">
                        Name cannot be Bob.
                      </div>
                    </div>
                  </div>
                </div>
                <!-- end form-group row -->
                <!-- begin form-group row -->
                <div class="form-group row m-b-10">
                  <div class="col-md-4 offset-md-2">
                    <!-- <input class="btn btn-sm btn-primary m-r-5" [disabled]="!(CustomerProfileModel.FormCommonGroup.valid)" (click)="AddCompany()" type=button value="Add Customer" /> -->
                    <input class="btn btn-sm btn-primary m-r-5" (click)="AddCompany()" type=button value="Add Customer" />
                    <a [routerLink]="['/CustomerProfile']" class="btn btn-sm btn-default">Cancel</a>
                  </div>
                </div>
              </div>
              <!-- end panel -->
            </div>                
          </div>
        </div>
      </form>

Here is my model file Customer.ts.

import {
  NgForm,
  FormGroup,
  FormControl,
  Validators,
  FormBuilder
} from '@angular/forms'

export class Customer {
  LegalName: string = "";
  FormCommonGroup: FormGroup = null;
  constructor() {
    var _builder = new FormBuilder();
    this.FormCommonGroup = _builder.group({}); //Use the builder to create

    //control --> validation and 1 validation
    this.FormCommonGroup.addControl("CommonLegalNameControl",
      new FormControl('', Validators.required));
  }
}

As i have called in below on the CustomerAdd.html.

<input type="text" name="CommonLegalNameControl" required minlength="4" placeholder="" class="form-control" formControlName="CommonLegalNameControl" [(ngModel)]="CustomerProfileModel.LegalName">
                <div *ngIf="CommonLegalNameControl?.invalid && (CommonLegalNameControl?.dirty || CommonLegalNameControl?.touched)" class="alert alert-danger">
                  <div *ngIf="CommonLegalNameControl?.errors.required">
                    Name is required.
                  </div>
                  <div *ngIf="CommonLegalNameControl?.errors.minlength">
                    Name must be at least 4 characters long.
                  </div>
                  <div *ngIf="CommonLegalNameControl.errors.forbiddenName">
                    Name cannot be Bob.
                  </div>
                </div>

which doesn't seem to work and i have followed same thing for click method as well.
https://stackblitz.com/edit/angular-7-reactive-form-validation?file=app%2Fapp.component.html
https://stackblitz.com/edit/angular-8-reactive-form-validation?file=app%2Fapp.component.html
Both of them are not working as expected in my form.

Update 1: Here is my CustomerAdd.ts file below.

import { Component, Input, OnInit, ViewEncapsulation } from '@angular/core';
import { Customer } from '../_models/Customer '
@Component({
  selector: 'CustomerProfileAdd',
  templateUrl: './CustomerProfileAdd.html',
  styleUrls: ['./CustomerProfileAdd.css'],
})

export class CustomerProfileAdd implements OnInit {
  public CustomerProfileModel: Customer = new Customer();
  constructor() {
  }

  async ngOnInit() {    
    await this.CustomerProfileModel;
  }

  AddCompany() {
    this.submitted = true;

    // stop here if form is invalid
    if (this.CustomerProfileModel.FormCommonGroup.invalid) {
      return;
    }
  }
}
1
a) What means "not working as expected"? b) Having the form definition on a model class seems to be the wrong place, it should be in your CustomerAdd.ts component class. b) How does you component class look like?Christoph Lütjen
The above two stackblitz links are working perfectly with validation whats not working for you?Kamran Khatti
@KamranKhatti these are not working in my form.Naman Kumar
@ChristophLütjen i'll update my CustomerAdd.ts files as well.Naman Kumar
@NamanKumar then you should add your example on stackblitz that could help us to debug, please move your code to stackblitz.Kamran Khatti

1 Answers

1
votes

As i again followed this url.
https://stackblitz.com/edit/angular-8-reactive-form-validation?file=app%2Fapp.component.html
Now as i was working my way through i missed some little points.

 <input type="text" formControlName="lastName" class="form-control" [ngClass]="{ 'is-invalid': submitted && f.lastName.errors }" />
                    <div *ngIf="submitted && f.lastName.errors" class="invalid-feedback">
                        <div *ngIf="f.lastName.errors.required">Last Name is required</div>
                    </div>

Everything i done was ok, as i missed this part. [ngClass]="{ 'is-invalid': submitted && f.lastName.errors }" in the input tag i had give this in order to work.And in the CustomerAdd.ts i gave this.

// convenience getter for easy access to form fields
    get f() { return this.CustomerProfileModel.FormCommonGroup.controls; }