Making entity birthday optionalble

This commit is contained in:
2023-03-14 14:56:14 +01:00
parent b1d0e115f4
commit b4f81431b9
3 changed files with 73 additions and 47 deletions

View File

@@ -23,10 +23,9 @@ class Individual(EntityType):
props={"items-per-row": "4", "numbered": True}, props={"items-per-row": "4", "numbered": True},
title="Surnoms" title="Surnoms"
) )
day_of_birth: date = Field(title='Date de naissance') day_of_birth: date = Field(default=None, title='Date de naissance')
place_of_birth: str = Field(default="", title='Lieu de naissance') place_of_birth: str = Field(default="", title='Lieu de naissance')
@property @property
def label(self) -> str: def label(self) -> str:
if len(self.surnames) > 0: if len(self.surnames) > 0:

View File

@@ -1,7 +1,6 @@
import { Component, OnInit } from '@angular/core';
import { Component, ElementRef, OnInit, ViewChild} from '@angular/core';
import { formatDate } from "@angular/common"; import { formatDate } from "@angular/common";
import { NgbDateStruct, NgbTimeStruct } from '@ng-bootstrap/ng-bootstrap'; import { NgbDateStruct } from '@ng-bootstrap/ng-bootstrap';
import { FieldType, FieldTypeConfig } from '@ngx-formly/core'; import { FieldType, FieldTypeConfig } from '@ngx-formly/core';
@@ -15,12 +14,13 @@ import { FieldType, FieldTypeConfig } from '@ngx-formly/core';
<div class="alert alert-danger" role="alert" *ngIf="showError && formControl.errors"> <div class="alert alert-danger" role="alert" *ngIf="showError && formControl.errors">
<formly-validation-message [field]="field"></formly-validation-message> <formly-validation-message [field]="field"></formly-validation-message>
</div> </div>
<input type="hidden"
[formControl]="formControl"
[formlyAttributes]="field"
[class.is-invalid]="showError"
/>
<div class="input-group" *ngIf="! this.field.props.readonly"> <div class="input-group" *ngIf="! this.field.props.readonly">
<input type="hidden" <button class="btn btn-outline-secondary" (click)="d.toggle()" type="button"><i-bs name="calendar-date-fill"></i-bs></button>
[formControl]="formControl"
[formlyAttributes]="field"
[class.is-invalid]="showError"
/>
<input <input
class="form-control" class="form-control"
placeholder="yyyy-mm-dd" placeholder="yyyy-mm-dd"
@@ -30,22 +30,20 @@ import { FieldType, FieldTypeConfig } from '@ngx-formly/core';
ngbDatepicker ngbDatepicker
#d="ngbDatepicker" #d="ngbDatepicker"
/> />
<button class="btn btn-outline-secondary" (click)="d.toggle()" type="button"><i-bs name="calendar-date-fill"></i-bs></button>
</div> </div>
<div class="input-group" *ngIf="this.field.props.readonly"> <div class="input-group" *ngIf="this.field.props.readonly">
<input class="form-control" value="{{ this.datetime.toLocaleString() }}" disabled=""/> <input class="form-control" value="{{ this.datetime ? this.datetime.toLocaleString() : '' }}" disabled=""/>
</div> </div>
`, `,
}) })
export class DateTypeComponent extends FieldType<FieldTypeConfig> implements OnInit export class DateTypeComponent extends FieldType<FieldTypeConfig> implements OnInit
{ {
public date : NgbDateStruct; public date : NgbDateStruct | null = null;
public datetime : Date = new Date(); public datetime : Date | null = null;
constructor() { constructor() {
super(); super();
this.date = this.getDateStruct(new Date());
} }
ngOnInit() { ngOnInit() {
@@ -54,8 +52,13 @@ export class DateTypeComponent extends FieldType<FieldTypeConfig> implements OnI
} }
this.formControl.valueChanges.subscribe(value => { this.formControl.valueChanges.subscribe(value => {
this.datetime = new Date(value) if (value) {
this.date = this.getDateStruct(this.datetime); this.datetime = new Date(value);
this.date = this.getDateStruct(this.datetime);
} else {
this.datetime = null;
this.date = null;
}
}) })
} }
@@ -68,12 +71,21 @@ export class DateTypeComponent extends FieldType<FieldTypeConfig> implements OnI
} }
changeDatetime(event: any) { changeDatetime(event: any) {
this.datetime.setFullYear(this.date.year) if (this.date) {
this.datetime.setMonth(this.date.month - 1) if (!this.datetime) {
this.datetime.setDate(this.date.day) this.datetime = new Date();
}
this.datetime.setFullYear(this.date.year)
this.datetime.setMonth(this.date.month - 1)
this.datetime.setDate(this.date.day)
this.formControl.setValue( this.formControl.setValue(
formatDate(this.datetime, 'YYYY-MM-dd', 'EN_US', 'CET') formatDate(this.datetime, 'YYYY-MM-dd', 'EN_US', 'CET')
) )
} else {
this.datetime = null;
this.date = null;
this.formControl.setValue('')
}
} }
} }

View File

@@ -1,5 +1,4 @@
import { Component, OnInit } from '@angular/core';
import { Component, ElementRef, OnInit, ViewChild} from '@angular/core';
import { formatDate } from "@angular/common"; import { formatDate } from "@angular/common";
import { NgbDateStruct, NgbTimeStruct } from '@ng-bootstrap/ng-bootstrap'; import { NgbDateStruct, NgbTimeStruct } from '@ng-bootstrap/ng-bootstrap';
import { FieldType, FieldTypeConfig } from '@ngx-formly/core'; import { FieldType, FieldTypeConfig } from '@ngx-formly/core';
@@ -12,12 +11,13 @@ import { FieldType, FieldTypeConfig } from '@ngx-formly/core';
class="form-label">{{ props.label }} class="form-label">{{ props.label }}
<span *ngIf="props.required && props['hideRequiredMarker'] !== true" aria-hidden="true">*</span> <span *ngIf="props.required && props['hideRequiredMarker'] !== true" aria-hidden="true">*</span>
</label> </label>
<input type="hidden"
[formControl]="formControl"
[formlyAttributes]="field"
[class.is-invalid]="showError"
/>
<div class="input-group" *ngIf="! this.field.props.readonly"> <div class="input-group" *ngIf="! this.field.props.readonly">
<input type="hidden" <button class="btn btn-outline-secondary bi bi-calendar3" (click)="d.toggle()" type="button"></button>
[formControl]="formControl"
[formlyAttributes]="field"
[class.is-invalid]="showError"
/>
<input <input
class="form-control" class="form-control"
placeholder="yyyy-mm-dd" placeholder="yyyy-mm-dd"
@@ -27,7 +27,6 @@ import { FieldType, FieldTypeConfig } from '@ngx-formly/core';
ngbDatepicker ngbDatepicker
#d="ngbDatepicker" #d="ngbDatepicker"
/> />
<button class="btn btn-outline-secondary bi bi-calendar3" (click)="d.toggle()" type="button"></button>
<ngb-timepicker <ngb-timepicker
(ngModelChange)="changeDatetime($event)" (ngModelChange)="changeDatetime($event)"
[(ngModel)]="time" [(ngModel)]="time"
@@ -35,15 +34,15 @@ import { FieldType, FieldTypeConfig } from '@ngx-formly/core';
</ngb-timepicker> </ngb-timepicker>
</div> </div>
<div class="input-group" *ngIf="this.field.props.readonly"> <div class="input-group" *ngIf="this.field.props.readonly">
<input class="form-control" value="{{ this.datetime.toLocaleString() }}" disabled=""/> <input class="form-control" value="{{ this.datetime ? this.datetime.toLocaleString() : '' }}" disabled=""/>
</div> </div>
`, `,
}) })
export class DatetimeTypeComponent extends FieldType<FieldTypeConfig> implements OnInit export class DatetimeTypeComponent extends FieldType<FieldTypeConfig> implements OnInit
{ {
public time : NgbTimeStruct; public time : NgbTimeStruct | null = null;
public date : NgbDateStruct; public date : NgbDateStruct | null = null;
public datetime : Date = new Date() public datetime : Date | null = null;
constructor() { constructor() {
@@ -58,9 +57,15 @@ export class DatetimeTypeComponent extends FieldType<FieldTypeConfig> implements
} }
this.formControl.valueChanges.subscribe(value => { this.formControl.valueChanges.subscribe(value => {
this.datetime = new Date(value) if (value) {
this.date = this.getDateStruct(this.datetime); this.datetime = new Date(value);
this.time = this.getTimeStruct(this.datetime); this.date = this.getDateStruct(this.datetime);
this.time = this.getTimeStruct(this.datetime);
} else {
this.datetime = null;
this.date = null;
this.time = null;
}
}) })
} }
@@ -81,15 +86,25 @@ export class DatetimeTypeComponent extends FieldType<FieldTypeConfig> implements
} }
changeDatetime(event: any) { changeDatetime(event: any) {
this.datetime.setFullYear(this.date.year) if (this.date && this.time) {
this.datetime.setMonth(this.date.month - 1) if (!this.datetime) {
this.datetime.setDate(this.date.day) this.datetime = new Date();
this.datetime.setHours(this.time.hour) }
this.datetime.setMinutes(this.time.minute) this.datetime.setFullYear(this.date.year)
this.datetime.setSeconds(this.time.second) this.datetime.setMonth(this.date.month - 1)
this.datetime.setDate(this.date.day)
this.datetime.setHours(this.time.hour)
this.datetime.setMinutes(this.time.minute)
this.datetime.setSeconds(this.time.second)
this.formControl.setValue( this.formControl.setValue(
formatDate(this.datetime, 'YYYY-MM-ddTHH:mm:ss.SSS', 'EN_US', 'CET') formatDate(this.datetime, 'YYYY-MM-ddTHH:mm:ss.SSS', 'EN_US', 'CET')
) )
} else {
this.datetime = null;
this.date = null;
this.time = null;
this.formControl.setValue('')
}
} }
} }