fix: form validator

This commit is contained in:
zxhlyh 2025-10-22 11:38:46 +08:00
parent 828b9c6a93
commit 1cf7007fb4
6 changed files with 54 additions and 7 deletions

View File

@ -88,6 +88,19 @@ const BaseForm = ({
})
return result
})
const moreOnValues = useStore(form.store, (s: any) => {
const result: Record<string, any> = {}
formSchemas.forEach((schema) => {
const { more_on } = schema
const moreOn = typeof more_on === 'function' ? more_on(form) : more_on
if (moreOn?.length) {
moreOn?.forEach((condition) => {
result[condition.variable] = s.values[condition.variable]
})
}
})
return result
})
useImperativeHandle(ref, () => {
return {
@ -104,11 +117,18 @@ const BaseForm = ({
const formSchema = formSchemas?.find(schema => schema.name === field.name)
if (formSchema) {
const { more_on = [] } = formSchema
const moreOn = typeof more_on === 'function' ? more_on(form) : more_on
const more = (moreOn || []).every((condition) => {
const conditionValue = moreOnValues[condition.variable]
return Array.isArray(condition.value) ? condition.value.includes(conditionValue) : conditionValue === condition.value
})
return (
<BaseField
field={field}
formSchema={formSchema}
fieldClassName={fieldClassName}
fieldClassName={cn(fieldClassName, !more ? 'absolute top-[-9999px]' : '')}
labelClassName={labelClassName}
inputContainerClassName={inputContainerClassName}
inputClassName={inputClassName}
@ -119,7 +139,7 @@ const BaseForm = ({
}
return null
}, [formSchemas, fieldClassName, labelClassName, inputContainerClassName, inputClassName, disabled, onChange])
}, [formSchemas, fieldClassName, labelClassName, inputContainerClassName, inputClassName, disabled, onChange, moreOnValues])
const renderFieldWrapper = useCallback((formSchema: FormSchema) => {
const validators = getValidators(formSchema)

View File

@ -15,13 +15,14 @@ export const useCheckValidated = (form: AnyFormApi, FormSchemas: FormSchema[]) =
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) => {
const showOn = typeof show_on === 'function' ? show_on(form) : show_on
const showOnValues = (showOn || []).reduce((acc, condition) => {
acc[condition.variable] = values[condition.variable]
return acc
}, {} as Record<string, any>)
const show = show_on?.every((condition) => {
const show = (showOn || []).every((condition) => {
const conditionValue = showOnValues[condition.variable]
return conditionValue === condition.value
return Array.isArray(condition.value) ? condition.value.includes(conditionValue) : conditionValue === condition.value
})
const errors: any[] = show ? fields[key].errors : []

View File

@ -62,6 +62,7 @@ export type FormSchema = {
default?: any
tooltip?: string | TypeWithI18N
show_on?: FormShowOnObject[] | ((form: AnyFormApi) => FormShowOnObject[])
more_on?: FormShowOnObject[] | ((form: AnyFormApi) => FormShowOnObject[])
url?: string
scope?: string
help?: string | TypeWithI18N

View File

@ -85,7 +85,17 @@ export const useNodesSyncDraft = () => {
},
environment_variables: environmentVariables,
conversation_variables: conversationVariables,
memory_blocks: memoryVariables.map(({ node, value_type, more, ...rest }) => rest),
memory_blocks: memoryVariables.map(({ node, value_type, more, model, ...rest }) => {
return {
...rest,
model: model ? {
completion_params: model.completion_params,
mode: model.mode,
provider: model.provider,
name: model.model,
} : undefined,
}
}),
hash: syncWorkflowDraftHash,
},
}

View File

@ -109,6 +109,12 @@ export const useFormatMemoryVariables = () => {
return {
...v,
value_type: ChatVarType.Memory,
model: v.model ? {
completion_params: v.model?.completion_params,
mode: v.model?.mode,
provider: v.model?.provider,
model: v.model?.name,
} : undefined,
}
})
}, [])

View File

@ -70,6 +70,7 @@ export const useMemorySchema = () => {
label: t('workflow.chatVariable.modal.updateEvery'),
type: FormTypeEnum.textNumber,
fieldClassName: 'flex justify-between',
required: true,
show_on: [
{
variable: 'value_type',
@ -175,11 +176,14 @@ export const useMemorySchema = () => {
name: 'model',
label: t('workflow.chatVariable.modal.memoryModel'),
type: FormTypeEnum.modelSelector,
required: true,
show_on: [
{
variable: 'value_type',
value: [ChatVarType.Memory],
},
],
more_on: [
{
variable: 'more',
value: true,
@ -205,6 +209,8 @@ export const useMemorySchema = () => {
variable: 'value_type',
value: [ChatVarType.Memory],
},
],
more_on: [
{
variable: 'more',
value: true,
@ -221,6 +227,8 @@ export const useMemorySchema = () => {
variable: 'value_type',
value: [ChatVarType.Memory],
},
],
more_on: [
{
variable: 'more',
value: true,
@ -235,7 +243,8 @@ export const useMemoryDefaultValues = () => {
template: '',
instruction: '',
strategy: 'on_turns',
update_turns: 0,
update_turns: 500,
preserved_turns: 10,
scope: 'app',
term: 'session',
more: false,