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;
}
}
}