0.0.0.3
This commit is contained in:
@@ -36,6 +36,8 @@ export function MemberForm({ initialData, existingMembers = [], onSubmit, onCanc
|
||||
...initialData,
|
||||
})
|
||||
|
||||
const [errors, setErrors] = useState<string[]>([])
|
||||
|
||||
const handleChange = (field: keyof FamilyMember, value: any) => {
|
||||
setFormData((prev) => {
|
||||
const newData = { ...prev, [field]: value }
|
||||
@@ -45,6 +47,8 @@ export function MemberForm({ initialData, existingMembers = [], onSubmit, onCanc
|
||||
}
|
||||
return newData
|
||||
})
|
||||
// Clear errors when user makes changes
|
||||
if (errors.length > 0) setErrors([])
|
||||
}
|
||||
|
||||
const handleSpouseChange = (spouseId: string) => {
|
||||
@@ -57,15 +61,81 @@ export function MemberForm({ initialData, existingMembers = [], onSubmit, onCanc
|
||||
setFormData((prev) => ({ ...prev, spouseIds: [spouseId] }))
|
||||
}
|
||||
|
||||
const validate = (): boolean => {
|
||||
const newErrors: string[] = []
|
||||
|
||||
// 1. Date Validation
|
||||
if (formData.birthDate && formData.deathDate) {
|
||||
if (new Date(formData.birthDate) > new Date(formData.deathDate)) {
|
||||
newErrors.push("出生日期不能晚于逝世日期")
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Parent Age Validation
|
||||
const birthYear = formData.birthDate ? new Date(formData.birthDate).getFullYear() : null
|
||||
|
||||
if (birthYear) {
|
||||
if (formData.fatherId) {
|
||||
const father = existingMembers.find((m) => m.id === formData.fatherId)
|
||||
if (father?.birthDate) {
|
||||
const fatherBirthYear = new Date(father.birthDate).getFullYear()
|
||||
if (birthYear - fatherBirthYear < 15) {
|
||||
newErrors.push(`与父亲的年龄差过小 (${birthYear - fatherBirthYear}岁),请确认`)
|
||||
}
|
||||
if (birthYear < fatherBirthYear) {
|
||||
newErrors.push("出生日期不能早于父亲")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (formData.motherId) {
|
||||
const mother = existingMembers.find((m) => m.id === formData.motherId)
|
||||
if (mother?.birthDate) {
|
||||
const motherBirthYear = new Date(mother.birthDate).getFullYear()
|
||||
if (birthYear - motherBirthYear < 15) {
|
||||
newErrors.push(`与母亲的年龄差过小 (${birthYear - motherBirthYear}岁),请确认`)
|
||||
}
|
||||
if (birthYear < motherBirthYear) {
|
||||
newErrors.push("出生日期不能早于母亲")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Generation Validation
|
||||
if (formData.fatherId) {
|
||||
const father = existingMembers.find((m) => m.id === formData.fatherId)
|
||||
if (father && formData.generation !== father.generation + 1) {
|
||||
newErrors.push(`世系错误:父亲是第 ${father.generation} 世,子女应为第 ${father.generation + 1} 世`)
|
||||
}
|
||||
}
|
||||
|
||||
setErrors(newErrors)
|
||||
return newErrors.length === 0
|
||||
}
|
||||
|
||||
const handleSubmit = (e: React.FormEvent) => {
|
||||
e.preventDefault()
|
||||
onSubmit(formData as FamilyMember)
|
||||
if (validate()) {
|
||||
onSubmit(formData as FamilyMember)
|
||||
}
|
||||
}
|
||||
|
||||
const potentialRelatives = existingMembers.filter((m) => m.id !== formData.id)
|
||||
|
||||
return (
|
||||
<form onSubmit={handleSubmit} className="space-y-8 max-w-4xl mx-auto p-6">
|
||||
{errors.length > 0 && (
|
||||
<div className="bg-destructive/15 text-destructive px-4 py-3 rounded-md border border-destructive/20">
|
||||
<p className="font-bold mb-1">请检查以下问题:</p>
|
||||
<ul className="list-disc list-inside text-sm">
|
||||
{errors.map((err, index) => (
|
||||
<li key={index}>{err}</li>
|
||||
))}
|
||||
</ul>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-6">
|
||||
{/* Basic Identity */}
|
||||
<Card>
|
||||
|
||||
Reference in New Issue
Block a user