Qualifikationen Bearbeiten #67
					 5 changed files with 41 additions and 3 deletions
				
			
		feat: add error handling for skill form validation
				commit
				
					
					
						703d644799
					
				
			
		|  | @ -104,7 +104,7 @@ export class MitarbeiterFormComponent { | ||||||
|     Object.keys(this.mitarbeiterForm.controls).forEach(field => { |     Object.keys(this.mitarbeiterForm.controls).forEach(field => { | ||||||
|       const control = this.mitarbeiterForm.get(field); |       const control = this.mitarbeiterForm.get(field); | ||||||
| 
 | 
 | ||||||
|       if (control && control.errors && control.touched) { |       if (control && control.errors) { | ||||||
|         this.errorMessages[field] = Object.keys(control.errors) |         this.errorMessages[field] = Object.keys(control.errors) | ||||||
|           .map(errorKey => this.validationErrorMessages[errorKey] || `Unknown error: ${errorKey}`) |           .map(errorKey => this.validationErrorMessages[errorKey] || `Unknown error: ${errorKey}`) | ||||||
|           .join(' '); |           .join(' '); | ||||||
|  |  | ||||||
|  | @ -3,6 +3,9 @@ | ||||||
|     <button class="back-button">Back</button> |     <button class="back-button">Back</button> | ||||||
| 
 | 
 | ||||||
|     <div class="form-group"> |     <div class="form-group"> | ||||||
|  |       @if (errorMessages['name']) { | ||||||
|  |       <div class="alert alert-danger">{{errorMessages['name']}}</div> | ||||||
|  |       } | ||||||
|       <label for="name">Name</label> |       <label for="name">Name</label> | ||||||
|       <input type="text" id="name" formControlName="name"> |       <input type="text" id="name" formControlName="name"> | ||||||
|     </div> |     </div> | ||||||
|  |  | ||||||
|  | @ -22,9 +22,29 @@ export class QualifikationFormComponent { | ||||||
|   public skillForm!: FormGroup; |   public skillForm!: FormGroup; | ||||||
|   public addableEmployees: Array<EmployeeResponseDTO> = []; |   public addableEmployees: Array<EmployeeResponseDTO> = []; | ||||||
|   public addedEmployees: Array<EmployeeNameDataDTO> = []; |   public addedEmployees: Array<EmployeeNameDataDTO> = []; | ||||||
|  |   errorMessages: Record<string, string> = {}; | ||||||
|  | 
 | ||||||
| 
 | 
 | ||||||
|   constructor(private skillService: SkillService, private employeeService: EmployeeService, private router: Router) { } |   constructor(private skillService: SkillService, private employeeService: EmployeeService, private router: Router) { } | ||||||
| 
 | 
 | ||||||
|  |   private validationErrorMessages: Record<string, string> = { | ||||||
|  |     required: "This field is required", | ||||||
|  |   }; | ||||||
|  | 
 | ||||||
|  |   updateErrorMessages(): void { | ||||||
|  |     this.errorMessages = {}; | ||||||
|  | 
 | ||||||
|  |     Object.keys(this.skillForm.controls).forEach(field => { | ||||||
|  |       const control = this.skillForm.get(field); | ||||||
|  | 
 | ||||||
|  |       if (control && control.errors) { | ||||||
|  |         this.errorMessages[field] = Object.keys(control.errors) | ||||||
|  |           .map(errorKey => this.validationErrorMessages[errorKey] || `Unknown error: ${errorKey}`) | ||||||
|  |           .join(' '); | ||||||
|  |       } | ||||||
|  |     }); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|   setUpForm() { |   setUpForm() { | ||||||
|     this.skillForm = new FormGroup({ |     this.skillForm = new FormGroup({ | ||||||
|       name: new FormControl(this.skill.skill, Validators.required), |       name: new FormControl(this.skill.skill, Validators.required), | ||||||
|  | @ -73,6 +93,11 @@ export class QualifikationFormComponent { | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   submit() { |   submit() { | ||||||
|  |     this.updateErrorMessages(); | ||||||
|  |     if (!this.skillForm.valid) { | ||||||
|  |       return; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     for (const employee of this.addedEmployees) { |     for (const employee of this.addedEmployees) { | ||||||
|       this.employeeService.getEmployeeById(employee.id).subscribe(employeeResponse => { |       this.employeeService.getEmployeeById(employee.id).subscribe(employeeResponse => { | ||||||
|         this.employeeService.addSkillToEmployee(this.skill.id, employeeResponse); |         this.employeeService.addSkillToEmployee(this.skill.id, employeeResponse); | ||||||
|  |  | ||||||
|  | @ -18,7 +18,7 @@ export class QualifikatonBearbeitenViewComponent { | ||||||
|   constructor(private skillService: SkillService, private route: ActivatedRoute) {} |   constructor(private skillService: SkillService, private route: ActivatedRoute) {} | ||||||
| 
 | 
 | ||||||
|   submitted(skill: QualificationGetDTO) { |   submitted(skill: QualificationGetDTO) { | ||||||
|     console.log(skill); |     this.skillService.updateSkill(skill); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   ngOnInit(): void { |   ngOnInit(): void { | ||||||
|  |  | ||||||
|  | @ -1,6 +1,6 @@ | ||||||
| import { HttpClient } from "@angular/common/http"; | import { HttpClient } from "@angular/common/http"; | ||||||
| import { Injectable } from "@angular/core"; | import { Injectable } from "@angular/core"; | ||||||
| import { EmployeesForAQualificationDTO, QualificationGetDTO } from "../models/skill"; | import { EmployeesForAQualificationDTO, QualificationGetDTO, QualificationPostDTO } from "../models/skill"; | ||||||
| import { Observable } from "rxjs"; | import { Observable } from "rxjs"; | ||||||
| import { EmployeeNameDataDTO } from "../models/mitarbeiter"; | import { EmployeeNameDataDTO } from "../models/mitarbeiter"; | ||||||
| 
 | 
 | ||||||
|  | @ -11,10 +11,20 @@ export class SkillService { | ||||||
| 
 | 
 | ||||||
|   public static readonly BASE_URL = "http://localhost:8089"; |   public static readonly BASE_URL = "http://localhost:8089"; | ||||||
| 
 | 
 | ||||||
|  |   getToPutDto(skill: QualificationGetDTO): QualificationPostDTO { | ||||||
|  |     return { | ||||||
|  |       skill: skill.skill, | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|   constructor(private http: HttpClient) { |   constructor(private http: HttpClient) { | ||||||
| 
 | 
 | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  |   updateSkill(skill: QualificationGetDTO) { | ||||||
|  |     this.http.put(`${SkillService.BASE_URL}/qualifications/${skill.id}`, this.getToPutDto(skill)).subscribe(); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|   getAllSkills(): Observable<Array<QualificationGetDTO>> { |   getAllSkills(): Observable<Array<QualificationGetDTO>> { | ||||||
|     return this.http.get<Array<QualificationGetDTO>>(`${SkillService.BASE_URL}/qualifications`); |     return this.http.get<Array<QualificationGetDTO>>(`${SkillService.BASE_URL}/qualifications`); | ||||||
|   } |   } | ||||||
|  |  | ||||||
		Reference in a new issue