validate date input

This commit is contained in:
Chneemann 2024-04-07 13:14:32 +02:00
parent 75b02685ec
commit 450a96d2de
3 changed files with 234 additions and 115 deletions

View file

@ -7,8 +7,8 @@
(ngSubmit)="onSubmit(taskForm)" (ngSubmit)="onSubmit(taskForm)"
#taskForm="ngForm" #taskForm="ngForm"
onsubmit="return false" onsubmit="return false"
class="content"
> >
<div class="content">
<div class="left-side"> <div class="left-side">
<div class="title"> <div class="title">
<p>Title<span class="red-dot">*</span></p> <p>Title<span class="red-dot">*</span></p>
@ -18,7 +18,9 @@
name="title" name="title"
#title="ngModel" #title="ngModel"
(input)="saveTaskData()" (input)="saveTaskData()"
placeholder="{{ taskData.title }}" placeholder="{{
taskData.title ? taskData.title : 'Enter a title'
}}"
[(ngModel)]="taskData.title" [(ngModel)]="taskData.title"
autocomplete="off" autocomplete="off"
required required
@ -38,7 +40,11 @@
#description="ngModel" #description="ngModel"
minlength="10" minlength="10"
(input)="saveTaskData()" (input)="saveTaskData()"
placeholder="{{ taskData.description }}" placeholder="{{
taskData.description
? taskData.description
: 'Enter a description'
}}"
[(ngModel)]="taskData.description" [(ngModel)]="taskData.description"
autocomplete="off" autocomplete="off"
required required
@ -59,7 +65,7 @@
id="date" id="date"
name="date" name="date"
#date="ngModel" #date="ngModel"
(input)="saveTaskData()" (input)="checkDateInput()"
placeholder="{{ taskData.date }}" placeholder="{{ taskData.date }}"
[(ngModel)]="taskData.date" [(ngModel)]="taskData.date"
autocomplete="off" autocomplete="off"
@ -68,6 +74,8 @@
<div class="error-msg"> <div class="error-msg">
@if (!date.valid && date.touched) { @if (!date.valid && date.touched) {
<p>You must enter a date.</p> <p>You must enter a date.</p>
} @if (dateInPast && date.valid) {
<p>The date must be in the future</p>
} }
</div> </div>
</div> </div>
@ -115,7 +123,8 @@
'btn-active': taskData.priority == 'low' 'btn-active': taskData.priority == 'low'
}" }"
[ngStyle]="{ [ngStyle]="{
'background-color': taskData.priority == 'low' ? 'green' : 'white' 'background-color':
taskData.priority == 'low' ? 'green' : 'white'
}" }"
(click)="tooglePriority('low')" (click)="tooglePriority('low')"
> >
@ -127,5 +136,29 @@
</div> </div>
</div> </div>
</div> </div>
</div>
<div class="form-buttons">
<input
class="btn-clear"
type="button"
value="Clear"
(click)="removeTaskData()"
/>
<input
class="btn-submit"
type="submit"
value="Create Task"
[disabled]="
title.invalid ||
!taskData.title ||
description.invalid ||
!taskData.description ||
date.invalid ||
!taskData.date ||
!taskData.priority ||
dateInPast
"
/>
</div>
</form> </form>
</section> </section>

View file

@ -111,3 +111,56 @@ p {
.btn:hover { .btn:hover {
box-shadow: 2px 2px 2px 0px rgba(0, 0, 0, 0.3); box-shadow: 2px 2px 2px 0px rgba(0, 0, 0, 0.3);
} }
/*------------- SUBMIT BUTTON -------------*/
.form-buttons {
display: flex;
justify-content: end;
}
.btn-submit {
width: 183px;
height: 56px;
padding: 12px;
margin: 12px 12px;
border-radius: 10px;
border: 1px solid var(--black) !important;
background-color: var(--light-gray);
font-size: 23px;
font-weight: 400;
transition: 125ms ease-in-out;
&:hover:not(:disabled) {
box-shadow: 2px 2px 2px 0px rgba(0, 0, 0, 0.3);
background-color: var(--light-blue) !important;
border: 1px solid var(--light-blue) !important;
cursor: pointer;
}
&:not(:disabled) {
background-color: var(--very-dark-blue) !important;
color: var(--white);
}
&:hover {
cursor: default;
}
}
.btn-clear {
width: 110px;
height: 56px;
padding: 12px;
margin: 12px 12px;
border-radius: 10px;
color: var(--black);
background-color: var(--white);
border: 1px solid var(--black);
font-size: 23px;
font-weight: 400;
transition: 125ms ease-in-out;
cursor: pointer;
&:hover {
color: var(--light-blue);
border-color: var(--light-bluek);
box-shadow: 2px 2px 2px 0px rgba(0, 0, 0, 0.3);
}
}

View file

@ -1,6 +1,6 @@
import { CommonModule } from '@angular/common'; import { CommonModule } from '@angular/common';
import { Component } from '@angular/core'; import { Component, ViewChild } from '@angular/core';
import { FormsModule, NgForm } from '@angular/forms'; import { FormsModule, NgForm, NgModel } from '@angular/forms';
@Component({ @Component({
selector: 'app-add-task', selector: 'app-add-task',
@ -10,7 +10,11 @@ import { FormsModule, NgForm } from '@angular/forms';
styleUrl: './add-task.component.scss', styleUrl: './add-task.component.scss',
}) })
export class AddTaskComponent { export class AddTaskComponent {
@ViewChild('title', { static: true }) titleField!: NgModel;
@ViewChild('description', { static: true }) descriptionField!: NgModel;
currentDate: string = new Date().toISOString().split('T')[0]; currentDate: string = new Date().toISOString().split('T')[0];
dateInPast: boolean = false;
taskData = { taskData = {
title: '', title: '',
@ -26,6 +30,17 @@ export class AddTaskComponent {
} }
} }
checkDateInput() {
const currentDateForm = this.taskData.date.replaceAll('-', '');
const currentDate = new Date()
.toISOString()
.split('T')[0]
.replaceAll('-', '');
currentDateForm < currentDate
? (this.dateInPast = true)
: (this.dateInPast = false);
}
tooglePriority(prio: string) { tooglePriority(prio: string) {
this.taskData.priority !== prio this.taskData.priority !== prio
? (this.taskData.priority = prio) ? (this.taskData.priority = prio)
@ -37,9 +52,27 @@ export class AddTaskComponent {
localStorage.setItem('taskData', JSON.stringify(this.taskData)); localStorage.setItem('taskData', JSON.stringify(this.taskData));
} }
removeTaskData() {
localStorage.removeItem('taskData');
this.untouchedFormFields();
this.clearFormData();
}
untouchedFormFields() {
this.titleField.control.markAsUntouched();
this.descriptionField.control.markAsUntouched();
}
clearFormData() {
this.taskData.title = '';
this.taskData.description = '';
this.taskData.date = this.currentDate;
}
onSubmit(ngForm: NgForm) { onSubmit(ngForm: NgForm) {
if (ngForm.submitted && ngForm.form.valid) { if (ngForm.submitted && ngForm.form.valid) {
console.log('Send completed'); console.log('Send completed');
this.removeTaskData();
} }
} }
} }