Here my scenario is i have 3 users
1. admin will have 3 fields email,firstname,lastname.
2. employee will have 4 fields email,firstname,lastname,contact.
3. frontOffice will have 5 fields email,firstname,lastname,airlineDetails,vendor,personNames.
stackblitz link -: https://stackblitz.com/edit/angular-material-forms-deborahk-jgxzic
based on condition i have to show these fields and set the values to these fields based on the here i followed a way where i am on disabling fields like below
this.userForm = new FormGroup({
email : new FormControl(null, Validators.email),
firstName : new FormControl(null, Validators.required),
lastName : new FormControl(null, Validators.required),
contact: new FormControl({value: '', disabled: false}, Validators.required),
airlineDetails: new FormControl({value: '', disabled: false}, Validators.required),
vendor: new FormControl({value: '', disabled: false}, Validators.required),
personNames: new FormControl({value: '', disabled: false}, Validators.required)
});
if(this.userOne=="admin"){
this.userForm.get('contact').disable();
this.userForm.get(' airlineDetails').disable();
this.userForm.get('vendor').disable();
this.userForm.get('personNames').disable();
}
if(this.userTwo=="employee"){
this.userForm.get('contact').enable();
this.userForm.get('airlineDetails').disable();
this.userForm.get('vendor').disable();
this.userForm.get('personNames').disable();
}
if(this.userTwo=="frontOffice"){
this.userForm.get('contact').disable();
this.userForm.get(' airlineDetails').enable();
this.userForm.get('vendor').enable();
this.userForm.get('personNames').enable();
}
}
is there any other way for these kind of hiding and showing the fields and later set the values in it
below is my code:
<mat-toolbar color="primary">
<button type="button" (click)=" userData('admin')">ADMIN</button>
<button type="button" (click)=" userData('employee')">EMPLOYEES</button>
<button type="button" (click)=" userData('frontOffice')">frontOffice</button>
</mat-toolbar>
<div class="container" >
<form [formGroup]=" userForm" (ngSubmit)="onClick()" class="form">
<!--Email-->
<mat-form-field class="form-element" (ngSubmit)="onClick()">
<input matInput type="email" placeholder="Email Address" formControlName="email">
</mat-form-field>
<!--First Name-->
<mat-form-field class="form-element">
<input matInput type="text" placeholder="First name" formControlName="firstName">
</mat-form-field>
<!--last Name-->
<mat-form-field class="form-element">
<input matInput type="text" placeholder="First name" formControlName="lastName">
</mat-form-field>
<!------------------------------------------------------------------->
<mat-form-field class="form-element">
<input matInput type="text" placeholder="contact" formControlName="contact">
</mat-form-field>
<!------------------------------------------------------------------->
<mat-form-field class="form-element">
<input matInput type="text" placeholder="airline details" formControlName="airlineDetails">
</mat-form-field>
<!------------------------------------------------------------------->
<mat-form-field class="form-element">
<input matInput type="text" placeholder="vendor" formControlName="vendor">
</mat-form-field>
<mat-form-field class="form-element">
<input matInput type="text" placeholder="persons Names" formControlName="personNames">
</mat-form-field>
<button type="submit" [disabled]="userForm.invalid">Submit</button>
</form>
</div>
.ts code
userForm: FormGroup;
titleAlert: string = 'This field is required';
post: any = '';
userOne="admin";
userTwo="employee";
userThree="frontOffice"
constructor(private formBuilder: FormBuilder) { }
ngOnInit() {
this.createForm();
}
createForm() {
this.userForm = new FormGroup({
email : new FormControl(null, Validators.email),
firstName : new FormControl(null, Validators.required),
lastName : new FormControl(null, Validators.required),
contact: new FormControl({value: '', disabled: false}, Validators.required),
airlineDetails: new FormControl({value: '', disabled: false}, Validators.required),
vendor: new FormControl({value: '', disabled: false}, Validators.required),
personNames: new FormControl({value: '', disabled: false}, Validators.required)
});
}
onClick(){
console.log(this.userForm.value)
}
userData(params){
if(this.userOne=="admin"){
this.userForm.get('contact').disable();
this.userForm.get(' airlineDetails').disable();
this.userForm.get('vendor').disable();
this.userForm.get('personNames').disable();
}
if(this.userTwo=="employee"){
this.userForm.get('contact').enable();
this.userForm.get('airlineDetails').disable();
this.userForm.get('vendor').disable();
this.userForm.get('personNames').disable();
}
if(this.userTwo=="frontOffice"){
this.userForm.get('contact').disable();
this.userForm.get(' airlineDetails').enable();
this.userForm.get('vendor').enable();
this.userForm.get('personNames').enable();
}
}
userData(params){ if(param=="admin"){..} if (param=="employee"){...}...}