Files
dify/web/app/components/base/form/hooks/use-check-validated.ts
yyh dfcc0f8863 refactor(dify-ui): finish primitive migration from web/base/ui to @langgenius/dify-ui (#35349)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2026-04-17 08:46:11 +00:00

38 lines
1.3 KiB
TypeScript

import type { AnyFormApi } from '@tanstack/react-form'
import type { FormSchema } from '@/app/components/base/form/types'
import { toast } from '@langgenius/dify-ui/toast'
import { useCallback } from 'react'
export const useCheckValidated = (form: AnyFormApi, FormSchemas: FormSchema[]) => {
const checkValidated = useCallback(() => {
const allError = form?.getAllErrors()
const values = form.state.values
if (allError) {
const fields = allError.fields
const errorArray = Object.keys(fields).reduce((acc: string[], key: string) => {
const currentSchema = FormSchemas.find(schema => schema.name === key)
const { show_on = [] } = currentSchema || {}
const showOnValues = show_on.reduce((acc, condition) => {
acc[condition.variable] = values[condition.variable]
return acc
}, {} as Record<string, any>)
const show = show_on?.every((condition) => {
const conditionValue = showOnValues[condition.variable]
return conditionValue === condition.value
})
const errors: any[] = show ? fields[key]!.errors : []
return [...acc, ...errors]
}, [] as string[])
if (errorArray.length) {
toast.error(errorArray[0])
return false
}
return true
}
return true
}, [form, FormSchemas])
return {
checkValidated,
}
}