Skip to content

Commit 10bdb38

Browse files
committed
8th-Major-commit: Added Form-Validation functionality
1 parent 01b6cce commit 10bdb38

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

Angular/to_do_list/src/app/subComponents/add-to-do-item/add-to-do-item.component.html

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
1-
<form class="mb-3 text-start border border-primary border-3 rounded-3" (ngSubmit)="onSubmit()">
1+
<form class="mb-3 text-start border border-primary border-3 rounded-3 form-validate" (ngSubmit)="onSubmit()" novalidate>
22
<div class="mb-3 ms-4 pt-2">
3-
<label for="taskTitle" class="text-primary"><h5> Title: </h5></label>
4-
<input type="text" class="mx-2 border border-primary border-3 bg-white rounded-3 w-50 min-vw-50"
5-
placeholder="For ex. Study" id="taskTitle" name="taskTitle" [(ngModel)]="taskTitle">
3+
<label for="taskTitle" class="text-primary"><h5> Title: </h5></label>
4+
<input type="text" class="mx-2 border border-primary border-3 bg-white rounded-3 w-50 min-vw-50"
5+
placeholder="For ex. Study" id="taskTitle" name="taskTitle"
6+
[(ngModel)]="taskTitle" required>
7+
<div class="valid-feedback mx-2">
8+
<h6>Seems fine!</h6>
9+
</div>
10+
<div class="invalid-feedback mx-2">
11+
<h6>Please provide a valid Title</h6>
12+
</div>
613
</div>
714
<div class="mb-3 ms-4">
815
<label for="taskDescription" class="text-primary align-top"><h5>Task Description: </h5></label>
916
<textarea rows="4" cols="35" id="taskDescription" name="taskDescription"
10-
class="d-block mx-2 border border-primary border-3 bg-white rounded-3 w-75 min-vw-75" [(ngModel)]="taskDescription">Description of task</textarea>
17+
class="d-block mx-2 border border-primary border-3 bg-white rounded-3 w-75 min-vw-75"
18+
[(ngModel)]="taskDescription" required>Description of task</textarea>
19+
<div class="valid-feedback mx-2">
20+
<h6>Seems fine!</h6>
21+
</div>
22+
<div class="invalid-feedback mx-2">
23+
<h6>Please provide a valid Description</h6>
24+
</div>
1125
</div>
1226
<div class="mb-3 ms-4">
1327
<button type="submit" class="btn btn-primary px-5 mx-2">Add task</button>

Angular/to_do_list/src/app/subComponents/add-to-do-item/add-to-do-item.component.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,40 @@ export class AddToDoItemComponent implements OnInit {
1919
constructor() { }
2020

2121
ngOnInit(): void {
22+
this.validateAddTodoForm();
23+
}
24+
25+
validateAddTodoForm(): void {
26+
// Fetch all the forms we want to apply custom Bootstrap validation styles to
27+
const forms = document.querySelectorAll('.form-validate')
28+
29+
// Loop over them and prevent submission
30+
Array.prototype.slice.call(forms)
31+
.forEach(function (form) {
32+
form.addEventListener('submit', function (event: { preventDefault: () => void; stopPropagation: () => void; }) {
33+
if (!form.checkValidity()) {
34+
event.preventDefault()
35+
event.stopPropagation()
36+
}
37+
38+
form.classList.add('was-validated')
39+
}, false)
40+
});
2241
}
2342

2443
//Emit task details to to-do-list component:
2544
onSubmit(){
26-
this.todo.title=this.taskTitle;
27-
this.todo.desc=this.taskDescription;
28-
this.addToDo.emit(this.todo);
45+
46+
if(this.taskTitle!== "" && this.taskDescription!== ""){
47+
this.todo.title=this.taskTitle;
48+
this.todo.desc=this.taskDescription;
49+
this.addToDo.emit(this.todo);
50+
this.taskTitle="";
51+
this.taskDescription="";
52+
}
53+
else{
54+
console.log("Add Todo Form is not filled with valid details.");
55+
}
2956
}
57+
3058
}

0 commit comments

Comments
 (0)