[api] Pass Date objects to IsoDateInput validators instead of string.

This commit is contained in:
Ferdinand Thiessen 2021-11-16 23:25:35 +01:00
parent 34fcdbdb7f
commit fca79c36ef
2 changed files with 18 additions and 3 deletions

View File

@ -54,7 +54,7 @@ export default defineComponent({
label: { type: String, default: 'Datum' },
readonly: Boolean,
rules: {
type: Array as PropType<Validator[]>,
type: Array as PropType<Validator<Date>[]>,
default: () => [],
},
},
@ -62,7 +62,22 @@ export default defineComponent({
setup(props, { emit, attrs }) {
const customRules = computed(() => [
props.type == 'date' ? stringIsDate : props.type == 'time' ? stringIsTime : stringIsDateTime,
...props.rules,
(value?: string) => {
if (props.rules.length > 0 && !!value) {
let date: Date | undefined = undefined;
if (props.type == 'date') date = modifyDate(value);
else if (props.type == 'time') date = modifyTime(value);
else {
const split = value.split(' ');
date = modifyTime(split[1], modifyDate(split[0]));
}
for (const rule of props.rules) {
const r = rule(date);
if (typeof r === 'string') return r;
}
return true;
}
},
]);
const clearable = computed(() =>

View File

@ -1,4 +1,4 @@
export type Validator = (value: unknown) => boolean | string;
export type Validator<T = unknown> = (value?: T | null) => boolean | string;
export function notEmpty(val: unknown) {
return !!val || 'Feld darf nicht leer sein!';