Validation
This commit is contained in:
parent
275d4b8737
commit
3d1750ecec
@ -1,5 +1,22 @@
|
|||||||
<form [formGroup]="qualificationForm" (ngSubmit)="create()">
|
<!-- create-qualification.component.html -->
|
||||||
<label for="skill-input">Skill</label>
|
<form [formGroup]="qualificationForm" (ngSubmit)="create()" class="space-y-4">
|
||||||
<input id="skill-input" type="text" formControlName="skill">
|
<div>
|
||||||
<button type="submit">create</button>
|
<label for="skill-input" class="block mb-1">Skill</label>
|
||||||
|
<input
|
||||||
|
id="skill-input"
|
||||||
|
type="text"
|
||||||
|
formControlName="skill"
|
||||||
|
class="w-full p-2 border rounded"
|
||||||
|
[ngClass]="{'border-red-500 focus:ring-0 focus:outline-none': isFieldInvalid('skill')}">
|
||||||
|
|
||||||
|
<div
|
||||||
|
*ngIf="isFieldInvalid('skill')"
|
||||||
|
class="text-sm text-red-500 mt-1">
|
||||||
|
{{ getErrorMessage('skill') }}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<button type="submit">
|
||||||
|
Create
|
||||||
|
</button>
|
||||||
</form>
|
</form>
|
||||||
|
@ -2,11 +2,14 @@ import {Component, inject} from '@angular/core';
|
|||||||
import {FormBuilder, ReactiveFormsModule, Validators} from "@angular/forms";
|
import {FormBuilder, ReactiveFormsModule, Validators} from "@angular/forms";
|
||||||
import QualificationService from "../services/qualification.service";
|
import QualificationService from "../services/qualification.service";
|
||||||
import {MatDialogRef} from "@angular/material/dialog";
|
import {MatDialogRef} from "@angular/material/dialog";
|
||||||
|
import {NgClass, NgIf} from "@angular/common";
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-create-qualification',
|
selector: 'app-create-qualification',
|
||||||
imports: [
|
imports: [
|
||||||
ReactiveFormsModule
|
ReactiveFormsModule,
|
||||||
|
NgClass,
|
||||||
|
NgIf
|
||||||
],
|
],
|
||||||
templateUrl: './create-qualification.component.html',
|
templateUrl: './create-qualification.component.html',
|
||||||
styleUrl: './create-qualification.component.css'
|
styleUrl: './create-qualification.component.css'
|
||||||
@ -20,6 +23,19 @@ export class CreateQualificationComponent {
|
|||||||
'skill': ['', Validators.required],
|
'skill': ['', Validators.required],
|
||||||
});
|
});
|
||||||
|
|
||||||
|
isFieldInvalid(fieldName: string): boolean {
|
||||||
|
const field = this.qualificationForm.get(fieldName);
|
||||||
|
return !!field?.invalid && (field.dirty || field.touched);
|
||||||
|
}
|
||||||
|
|
||||||
|
getErrorMessage(fieldName: string): string {
|
||||||
|
const field = this.qualificationForm.get(fieldName);
|
||||||
|
if (field?.errors?.['required']) {
|
||||||
|
return 'This field is required';
|
||||||
|
}
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
create() {
|
create() {
|
||||||
if (!this.qualificationForm.valid) {
|
if (!this.qualificationForm.valid) {
|
||||||
console.error('Validation failed');
|
console.error('Validation failed');
|
||||||
|
Reference in New Issue
Block a user