In the following multi part form handling …tell me what i need to change if I allow images/doc uploads for awards data. : import React, { useState } from 'react';
import { useForm, FormProvider, useFormContext } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import { FullFormData, FullFormSchema, BusinessInfo, ContactPerson, Address, telephone } from '../types/form';
import { Award, Program, FormPartSchema } from '../types/form';
// --- Child Components (defined below) ---
import BusinessInfoForm from '../components/BusinessInfoForm';
import OfficeAddressForm from '../components/OfficeAddressForm';
import WarehouseAddressForm from '../components/WarehouseAddressForm';
import TelephoneForm from '../components/TelephoneForm';
import ContactPersonsForm from '../components/ContactPersonsForm';
import AwardsForm from '../components/AwardsForm';
import ProgramsForm from '../components/ProgramsForm';
// React Hook Form instance for the entire form, with validation based on the FULL form schema
const methods = useForm({
resolver: zodResolver(FullFormSchema),
defaultValues: initialFormData,
mode: 'onBlur', # // Validate on blur for better UX
});
const {
handleSubmit,
trigger, # // Used to manually trigger validation for a specific part
getValues, # // To get current values without re-rendering
formState: { errors, isValidating }, # // Access form errors
setError, # // To manually set errors
clearErrors, # // To clear errors
} = methods;
# // Manually trigger validation for the current part of the form
# // We need to extract the relevant part of the data to validate against its specific schema
const valuesForCurrentStep = getValues();
let dataToValidate: any = {};
if (currentPartName === 'business_info') {
dataToValidate = { business_info: valuesForCurrentStep.business_info };
} else if (currentPartName ==='office_address') {
dataToValidate = { office_address: valuesForCurrentStep.office_address }; // Wrap in object to match schema
} else if (currentPartName ==='warehouse_address') {
dataToValidate = { warehouse_address: valuesForCurrentStep.warehouse_address }; // Wrap in object to match schema
} else if (currentPartName ==='telephone') {
dataToValidate = { telephone: valuesForCurrentStep.telephone }; // Wrap in object to match schema
} else if (currentPartName === 'contact_persons') {
dataToValidate = { contact_persons: valuesForCurrentStep.contact_persons }; // Wrap in object to match schema
} else if (currentPartName === 'awards') {
dataToValidate = { awards: valuesForCurrentStep.awards }; // Wrap in object to match schema
} else if (currentPartName === 'programs') {
dataToValidate = { programs: valuesForCurrentStep.programs }; // Wrap in object to match schema
}
try {
await currentPartSchema.parseAsync(dataToValidate);
# // If validation passes, update global state and proceed
setFormData(getValues()); # // Update the master form data with the latest values
setCurrentStep((prev) => prev + 1);
clearErrors(); # // Clear any previous step-level errors
} catch (error: any) {
if (error.issues) {
error.issues.forEach((issue: any) => {
# // Map Zod errors to RHF errors
const fieldName = `${currentPartName}.${issue.path[issue.path.length - 1]}`; // Simple path mapping
# // If it's an array field, map it correctly. e.g., contactPersons.0.firstName
if (issue.path.length > 1 && typeof issue.path[0] === 'string' && typeof issue.path[1] === 'number') {
setError(`${issue.path[0]}.${issue.path[1]}.${issue.path[2]}` as any, {
type: 'manual',
message: issue.message,
});
} else {
setError(issue.path.join('.') as any, { // For direct fields like business_info.business_name
type: 'manual',
message: issue.message,
});
}
});
}
console.error("Validation error:", error);
}
To allow image/document uploads for awards data in your React form, you need to make several key changes across your codebase. These changes ensure that the file is properly handled during form submission, the form state is updated correctly, and the server receives the data in a compatible format.
✅ 1. Update the Award Type in types/form.ts
Ensure the award_file field is typed as File | null instead of File (which is a constructor, not a value).
ts
// types/form.ts
export type Award = {
id: string;
award_name: string;
year: number;
issuing_organization: string;
award_file: File | null; // Changed from `File` to `File | null`
award_path: string;
};
✅ 2. Fix Initial Form Data (initialFormData) for Awards
Replace award_file: File with award_file: null to avoid invalid initial values.
If you want to show a preview or validate file types, you can enhance the AwardsForm component with additional logic.
🧪 Summary of Changes
Section
Change
Award type
Changed award_file to `File
initialFormData
Fixed award_file to null
onSubmit
Replaced JSON.stringify for awards with loop to append files
AwardsForm
Added file input for each award
Validation Schema
Excluded award_file from awards validation
With these changes, your form will now support uploading images/documents for awards, while maintaining compatibility with your backend (assuming it expects form data with nested fields and files).