add form to create employee

This commit is contained in:
2025-01-08 10:03:26 +01:00
parent 10ead075a7
commit d96a0342d0
6 changed files with 104 additions and 1 deletions

View File

@ -0,0 +1,49 @@
import {Component, inject, OnInit} from '@angular/core';
import {FormControl, FormGroup, ReactiveFormsModule, Validators} from "@angular/forms";
import {MatFormField, MatLabel} from "@angular/material/form-field";
import {MatInput} from "@angular/material/input";
import {MatButton} from "@angular/material/button";
import {MatDialogActions, MatDialogContent, MatDialogTitle} from "@angular/material/dialog";
import {Employee} from "../Employee";
import EmployeeApiService from "../services/employee-api.service";
@Component({
selector: 'app-create-employee',
imports: [
ReactiveFormsModule,
MatFormField,
MatInput,
MatButton,
MatLabel,
MatDialogContent,
MatDialogTitle,
MatDialogActions
],
templateUrl: './create-employee.component.html',
standalone: true,
styleUrl: './create-employee.component.css'
})
export class CreateEmployeeComponent implements OnInit{
employeeForm!: FormGroup;
employeeService: EmployeeApiService = inject(EmployeeApiService);
ngOnInit(): void {
this.employeeForm = new FormGroup({
firstName: new FormControl('', {validators: [Validators.required]}),
lastName: new FormControl('', {validators: [Validators.required]}),
street: new FormControl('', {validators: [Validators.required]}),
postcode: new FormControl('', {validators: [Validators.required, Validators.minLength(5), Validators.maxLength(5)]}),
city: new FormControl('', {validators: [Validators.required]}),
phone: new FormControl('', {validators: [Validators.required]}),
});
}
submit() {
if (!this.employeeForm.valid) {
console.error('Form invalid');
return;
}
this.employeeService.create(this.employeeForm.value as Employee).subscribe();
}
}