เพิ่ม validateForm

This commit is contained in:
DESKTOP-1R2VSQH\Lenovo ThinkPad E490 2023-11-08 13:44:03 +07:00
parent 8453e20234
commit 782fa7f59f
2 changed files with 118 additions and 44 deletions

View file

@ -1,10 +1,12 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref, onMounted, defineProps } from "vue"; import { ref, watch, onMounted, defineProps } from "vue";
import { useQuasar } from "quasar"; import { useQuasar } from "quasar";
import moment, { Moment } from "moment"; import moment, { Moment } from "moment";
// importStores // importStores
import { useCounterMixin } from "@/stores/mixin"; import { useCounterMixin } from "@/stores/mixin";
// importType
import type { FormRef } from "@/interface/index/Main";
const $q = useQuasar(); const $q = useQuasar();
const mixin = useCounterMixin(); const mixin = useCounterMixin();
@ -45,12 +47,52 @@ function updateClock() {
timeNoew.value = `${hh}:${mm} น.`; timeNoew.value = `${hh}:${mm} น.`;
} }
const dateRef = ref<object | null>(null);
const reasonRef = ref<object | null>(null);
const objectRef: FormRef = {
date: dateRef,
reason: reasonRef,
};
const checkstatusBox = ref<boolean>(false);
function onCkickSave() { function onCkickSave() {
dialogConfirm($q, async () => { const hasError = [];
console.log("save"); for (const key in objectRef) {
props.closePopup(); if (Object.prototype.hasOwnProperty.call(objectRef, key)) {
}); const property = objectRef[key];
if (property.value && typeof property.value.validate === "function") {
const isValid = property.value.validate();
hasError.push(isValid);
}
}
}
if (checkboxIn.value === false && checkboxOut.value === false) {
checkstatusBox.value = true;
}
if (
hasError.every(
(result) => result === true && checkstatusBox.value === false
)
) {
dialogConfirm($q, async () => {
console.log("save");
props.closePopup();
});
} else {
console.log("ไม่ผ่าน ");
}
} }
watch(
[() => checkboxIn.value, () => checkboxOut.value],
([newCheckboxIn, newCheckboxOut]) => {
if (checkstatusBox.value) {
if (newCheckboxIn || newCheckboxOut) {
checkstatusBox.value = false;
}
}
}
);
</script> </script>
<template> <template>
<q-card-section class="col q-pt-none"> <q-card-section class="col q-pt-none">
@ -60,11 +102,11 @@ function onCkickSave() {
bordered bordered
:class="$q.screen.gt.xs ? 'col-12 bg-grey-1' : 'col-12 '" :class="$q.screen.gt.xs ? 'col-12 bg-grey-1' : 'col-12 '"
> >
<q-card-section class="bg-primary text-white" v-if="!$q.screen.gt.xs"> <q-card-section class="bg-primary text-white q-pa-sm">
<div class="text-center">เวลาปจจ</div> <div class="text-center text-bold">เวลาปจจ</div>
</q-card-section> </q-card-section>
<div v-else class="q-pa-sm text-primary">เวลาปจจ</div> <!-- <div class="q-pa-sm text-primary">เวลาปจจ</div> -->
<q-card-section class="text-center"> <q-card-section class="text-center q-pa-sm">
<div class="row q-gutter-md"> <div class="row q-gutter-md">
<div class="col">{{ date2Thai(dateNow) }}</div> <div class="col">{{ date2Thai(dateNow) }}</div>
<div class="col">{{ timeNoew }}</div> <div class="col">{{ timeNoew }}</div>
@ -73,38 +115,43 @@ function onCkickSave() {
</q-card> </q-card>
<q-card flat bordered class="col-12 q-mt-sm" v-if="statusAction"> <q-card flat bordered class="col-12 q-mt-sm" v-if="statusAction">
<VueDatePicker <div class="q-pa-sm">
v-model="date" <VueDatePicker
:locale="'th'" v-model="date"
autoApply :locale="'th'"
borderless autoApply
:enableTimePicker="false" :enableTimePicker="false"
week-start="0" week-start="0"
> outlined
<template #year="{ year }"> >
{{ year + 543 }} <template #year="{ year }">
</template> {{ year + 543 }}
<template #year-overlay-value="{ value }"> </template>
{{ parseInt(value + 543) }} <template #year-overlay-value="{ value }">
</template> {{ parseInt(value + 543) }}
<template #trigger> </template>
<q-input <template #trigger>
borderless <q-input
dense ref="dateRef"
:model-value="date !== null ? date2Thai(date) : null" outlined
:label="`${'กรอกวันที่'}`" dense
> :model-value="date !== null ? date2Thai(date) : null"
<!-- :rules="[(val) => !!val || 'Field is required']" --> :label="`${'กรอกวันที่'}`"
<template v-slot:prepend> :rules="[(val) => !!val || 'กรุณาเลือกวันที่']"
<q-icon lazy-rules
name="event" hide-bottom-space
class="cursor-pointer" >
style="color: var(--q-primary)" <template v-slot:prepend>
> <q-icon
</q-icon> name="event"
</template> class="cursor-pointer"
</q-input> </template style="color: var(--q-primary)"
></VueDatePicker> >
</q-icon>
</template>
</q-input> </template
></VueDatePicker>
</div>
</q-card> </q-card>
<q-card flat bordered class="q-pa-sm col-12 bg-grey-1 q-mt-sm" v-else> <q-card flat bordered class="q-pa-sm col-12 bg-grey-1 q-mt-sm" v-else>
@ -115,7 +162,16 @@ function onCkickSave() {
<div class="col">{{ covertDateObject(dataById.date) }}</div> <div class="col">{{ covertDateObject(dataById.date) }}</div>
</div> </div>
</q-card> </q-card>
<q-card flat bordered class="q-pa-sm col-12 q-mt-sm">
<q-card
flat
bordered
class="q-pa-sm col-12 q-mt-sm"
:style="{
borderColor: checkstatusBox ? '#B22222' : '',
borderWidth: checkstatusBox ? '2px' : '',
}"
>
<div class="row q-gutter-xs"> <div class="row q-gutter-xs">
<div class="col-12"> <div class="col-12">
<q-checkbox <q-checkbox
@ -135,14 +191,27 @@ function onCkickSave() {
</div> </div>
</div> </div>
</q-card> </q-card>
<div
v-if="checkstatusBox"
class="text-red-9 q-pa-sm"
style="font-size: 10px"
>
กรณาเลอก
</div>
<q-card flat bordered class="q-pa-sm col-12 q-mt-sm"> <q-card flat bordered class="q-pa-sm col-12 q-mt-sm">
<q-input <q-input
borderless ref="reasonRef"
dense
outlined
v-model="reason" v-model="reason"
label="เหตุผล" label="เหตุผล"
type="textarea" type="textarea"
:rows="$q.screen.gt.xs ? '5' : '1'" :rows="$q.screen.gt.xs ? '5' : '1'"
label-color="grey-5" label-color="grey-5"
:rules="[(val) => !!val || 'กรุณากรอกเหตุผล']"
lazy-rules
hide-bottom-space
class="custom-aqua-border"
/> />
</q-card> </q-card>
</div> </div>

View file

@ -2,4 +2,9 @@ interface DataOption {
id: string; id: string;
name: string; name: string;
} }
export type { DataOption }; interface FormRef {
date: object | null;
reason: object | null;
[key: string]: any;
}
export type { DataOption, FormRef };