Merge pull request 'Added more strict form validation rules with user feedback' (#51) from Training-Report-Validation into main
Some checks failed
Continuous Deployment / Update Deployment (push) Failing after 1m11s
Some checks failed
Continuous Deployment / Update Deployment (push) Failing after 1m11s
Reviewed-on: #51
This commit was merged in pull request #51.
This commit is contained in:
@@ -19,5 +19,42 @@ export const trainingReportSchema = z.object({
|
||||
),
|
||||
remarks: z.string().nullable().optional(),
|
||||
attendees: z.array(courseEventAttendeeSchema).default([]),
|
||||
}).superRefine((data, ctx) => {
|
||||
const trainerRole = 1;
|
||||
const traineeRole = 2;
|
||||
|
||||
const hasTrainer = data.attendees.some((a) => a.attendee_role_id === trainerRole);
|
||||
const hasTrainee = data.attendees.some((a) => a.attendee_role_id === traineeRole);
|
||||
|
||||
if (!hasTrainer) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
path: ["attendees"],
|
||||
message: "At least one Primary Trainer is required.",
|
||||
});
|
||||
}
|
||||
|
||||
if (!hasTrainee) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
path: ["attendees"],
|
||||
message: "At least one Trainee is required.",
|
||||
});
|
||||
}
|
||||
|
||||
//no duplicates
|
||||
const idCounts = new Map<number, number>();
|
||||
|
||||
data.attendees.forEach((a, index) => {
|
||||
idCounts.set(a.attendee_id, (idCounts.get(a.attendee_id) ?? 0) + 1);
|
||||
|
||||
if (idCounts.get(a.attendee_id)! > 1) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
path: ["attendees"],
|
||||
message: "Cannot have duplicate attendee.",
|
||||
});
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
@@ -18,10 +18,12 @@ import FieldSet from '../ui/field/FieldSet.vue'
|
||||
import FieldLegend from '../ui/field/FieldLegend.vue'
|
||||
import FieldDescription from '../ui/field/FieldDescription.vue'
|
||||
import Checkbox from '../ui/checkbox/Checkbox.vue'
|
||||
import { configure } from 'vee-validate'
|
||||
|
||||
|
||||
const { handleSubmit, resetForm, errors, values, setFieldValue } = useForm({
|
||||
validationSchema: toTypedSchema(trainingReportSchema),
|
||||
validateOnMount: false,
|
||||
initialValues: {
|
||||
course_id: null,
|
||||
event_date: "",
|
||||
@@ -129,8 +131,17 @@ onMounted(async () => {
|
||||
|
||||
<VeeFieldArray name="attendees" v-slot="{ fields, push, remove }">
|
||||
<FieldSet class="gap-4">
|
||||
<FieldLegend class="scroll-m-20 text-lg tracking-tight">Attendees</FieldLegend>
|
||||
<FieldDescription>Add members who attended this session.</FieldDescription>
|
||||
<div class="flex flex-col gap-2">
|
||||
<FieldLegend class="scroll-m-20 text-lg tracking-tight mb-0">Attendees</FieldLegend>
|
||||
<FieldDescription class="mb-0">Add members who attended this session.</FieldDescription>
|
||||
<div class="h-4">
|
||||
<div class="text-red-500 text-sm"
|
||||
v-if="errors.attendees && typeof errors.attendees === 'string'">
|
||||
{{ errors.attendees }}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
<FieldGroup class="gap-4">
|
||||
|
||||
|
||||
Reference in New Issue
Block a user