I am trying to create a simple user registration project in angular.I'm trying to validate that no field is left empty in the form.However I'm unable to do so.
Here is my ts code:
export class AdduserComponent implements OnInit {
constructor(private formBuilder:FormBuilder,private userService:UsersService,private router:Router) { }
addForm: FormGroup;
selected = 'option2';
ngOnInit() {
this.addForm = this.formBuilder.group({
id: [],
userName: ['', Validators.required],
password: ['', Validators.required],
userRole:['',Validators.required],
});
}
onSubmit() {
this.userService.createUser(this.addForm.value)
.subscribe( data => {
this.router.navigate(['adduser']);
});
console.log(this.addForm.value);
}
changeClient(value) {
console.log(value);
}
}
Here is my template:
<form [formGroup]="addForm" class="login-form" (ngSubmit)="onSubmit()">
<div class="form-group">
<mat-form-field class="example-full-width">
<input matInput type="text" formControlName="userName" placeholder="userName" name="userName" class="form-control" id="userName">
</mat-form-field>
</div>
<div class="form-group">
<mat-form-field class="example-full-width">
<input matInput type="password" formControlName="password" placeholder="Password" name="password" class="form-control" id="password">
</mat-form-field>
</div>
<mat-form-field>
<mat-select panelClass="example-panel-blue.mat-select-panel" placeholder="Roles" formControlName="userRole" id="userRole" (selectionChange)="changeClient($event.value)" [(value)]="selected">
<mat-option value="Admin">Admin</mat-option>
</mat-select>
</mat-form-field>
<div class="container" style="margin: 12px" fxLayout="row" fxLayoutAlign="center center">
<button mat-raised-button color="primary">Create</button>
</div>
</form>
I've added validators but still I'm able to create user with null values.Please help.Thanks in advance