diff --git a/client/src/components/DogForm.jsx b/client/src/components/DogForm.jsx index 0d741aa..750821a 100644 --- a/client/src/components/DogForm.jsx +++ b/client/src/components/DogForm.jsx @@ -12,9 +12,9 @@ function DogForm({ dog, onClose, onSave }) { color: '', microchip: '', notes: '', - sire_id: '', - dam_id: '', - litter_id: '' + sire_id: null, // Changed from '' to null + dam_id: null, // Changed from '' to null + litter_id: null // Changed from '' to null }) const [dogs, setDogs] = useState([]) const [litters, setLitters] = useState([]) @@ -36,9 +36,9 @@ function DogForm({ dog, onClose, onSave }) { color: dog.color || '', microchip: dog.microchip || '', notes: dog.notes || '', - sire_id: dog.sire?.id || '', - dam_id: dog.dam?.id || '', - litter_id: dog.litter_id || '' + sire_id: dog.sire?.id || null, // Ensure null, not '' + dam_id: dog.dam?.id || null, // Ensure null, not '' + litter_id: dog.litter_id || null // Ensure null, not '' }) setUseManualParents(!dog.litter_id) } @@ -75,7 +75,14 @@ function DogForm({ dog, onClose, onSave }) { const handleChange = (e) => { const { name, value } = e.target - setFormData(prev => ({ ...prev, [name]: value })) + + // Convert empty strings to null for ID fields + let processedValue = value + if (name === 'sire_id' || name === 'dam_id' || name === 'litter_id') { + processedValue = value === '' ? null : parseInt(value) + } + + setFormData(prev => ({ ...prev, [name]: processedValue })) // If litter is selected, auto-populate parents if (name === 'litter_id' && value) { @@ -97,11 +104,17 @@ function DogForm({ dog, onClose, onSave }) { setLoading(true) try { - const submitData = { ...formData } - - // Clear litter_id if using manual parent selection - if (useManualParents) { - submitData.litter_id = null + const submitData = { + ...formData, + // Ensure null values are sent, not empty strings + sire_id: formData.sire_id || null, + dam_id: formData.dam_id || null, + litter_id: useManualParents ? null : (formData.litter_id || null), + registration_number: formData.registration_number || null, + birth_date: formData.birth_date || null, + color: formData.color || null, + microchip: formData.microchip || null, + notes: formData.notes || null } if (dog) { @@ -254,7 +267,7 @@ function DogForm({ dog, onClose, onSave }) { @@ -292,7 +305,7 @@ function DogForm({ dog, onClose, onSave }) {