File tree Expand file tree Collapse file tree 2 files changed +50
-8
lines changed
Angular/to_do_list/src/app/subComponents/add-to-do-item Expand file tree Collapse file tree 2 files changed +50
-8
lines changed Original file line number Diff line number Diff line change 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 >
2
2
< 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 >
6
13
</ div >
7
14
< div class ="mb-3 ms-4 ">
8
15
< label for ="taskDescription " class ="text-primary align-top "> < h5 > Task Description: </ h5 > </ label >
9
16
< 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 >
11
25
</ div >
12
26
< div class ="mb-3 ms-4 ">
13
27
< button type ="submit " class ="btn btn-primary px-5 mx-2 "> Add task</ button >
Original file line number Diff line number Diff line change @@ -19,12 +19,40 @@ export class AddToDoItemComponent implements OnInit {
19
19
constructor ( ) { }
20
20
21
21
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
+ } ) ;
22
41
}
23
42
24
43
//Emit task details to to-do-list component:
25
44
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
+ }
29
56
}
57
+
30
58
}
You can’t perform that action at this time.
0 commit comments