[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 50fba1a909
2 changed files with 17 additions and 3 deletions

View File

@ -54,7 +54,7 @@ export default defineComponent({
label: { type: String, default: 'Datum' }, label: { type: String, default: 'Datum' },
readonly: Boolean, readonly: Boolean,
rules: { rules: {
type: Array as PropType<Validator[]>, type: Array as PropType<Validator<Date>[]>,
default: () => [], default: () => [],
}, },
}, },
@ -62,7 +62,21 @@ export default defineComponent({
setup(props, { emit, attrs }) { setup(props, { emit, attrs }) {
const customRules = computed(() => [ const customRules = computed(() => [
props.type == 'date' ? stringIsDate : props.type == 'time' ? stringIsTime : stringIsDateTime, props.type == 'date' ? stringIsDate : props.type == 'time' ? stringIsTime : stringIsDateTime,
...props.rules, (value?: string | null) => {
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]));
}
props.rules.forEach((rule) => {
const r = rule(date);
if (typeof r === 'string') return r;
});
}
},
]); ]);
const clearable = computed(() => 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) { export function notEmpty(val: unknown) {
return !!val || 'Feld darf nicht leer sein!'; return !!val || 'Feld darf nicht leer sein!';