Skip to content

Commit

Permalink
修正表单校验bug:schema参数传错,导致不能校验
Browse files Browse the repository at this point in the history
  • Loading branch information
codebdy committed Dec 19, 2023
1 parent aa38c4d commit b69d84c
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 8 deletions.
3 changes: 1 addition & 2 deletions packages/fieldy/fieldy-react/src/components/Field/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ export const Field = (
}
) => {
const { name, value, initialValue, defaultValue, children, type, rules } = props
const fieldMeta = useCreateFieldSchema(name, type)
fieldMeta.validateRules = rules
const fieldMeta = useCreateFieldSchema(name, type, rules)

return (
<XField fieldMeta={fieldMeta} initialValue={initialValue} value={value} defaultValue={defaultValue}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { IFieldSchema } from "@rxdrag/fieldy"
import { useMemo, useRef } from "react"
import { useFieldPath } from "../../../hooks/useFieldPath"
import { FieldType } from "@rxdrag/fieldy"
import { IYupValidateSchema } from "@rxdrag/fieldy-yup-validation"

export function useCreateFieldSchema(name: string | number, type?: FieldType) {
export function useCreateFieldSchema(name: string | number, type?: FieldType, rules?: IYupValidateSchema) {
const parentPath = useFieldPath() || ""
const fieldPath = useMemo(() => {
return parentPath ? parentPath + "." + name : name.toString()
Expand All @@ -15,9 +16,10 @@ export function useCreateFieldSchema(name: string | number, type?: FieldType) {
return {
path: fieldPath,
type: type,
name
name,
validateRules: rules,
}
}, [fieldPath, name, type])
}, [fieldPath, name, rules, type])

return fieldSchema
}
1 change: 1 addition & 0 deletions packages/fieldy/fieldy/src/classes/FormImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export class FormImpl implements IForm {

try {
const value = await this.fieldy.validator.validateForm(this)

this.validationSubscriber.emitSuccess(value)
return {
status: ValidateStatus.success,
Expand Down
5 changes: 2 additions & 3 deletions packages/fieldy/yup-validation/src/classes/YupValidator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IField, IFieldSchema, IForm, IValidationError, IValidator } from "@rxdrag/fieldy";
import { PredeinedValidators, YupType, IYupValidateSchema } from "../interfaces";
import { object, string, boolean, number, ValidationError, Schema, mixed } from 'yup';
import { object, ValidationError, Schema, mixed } from 'yup';
import { predifinedValidators } from "../predefineds";

export class YupValidator implements IValidator {
Expand All @@ -17,8 +17,7 @@ export class YupValidator implements IValidator {
this.validateOneObject(field?.getValue(), field?.getSubFieldSchemas() || [])
}
}
const rootSchemas = form.getRootFields()
return this.validateOneObject(form.getValue(), rootSchemas)
return this.validateOneObject(form.getValue(), schemas)
}

validateField(field: IField<IYupValidateSchema>): Promise<unknown> {
Expand Down

0 comments on commit b69d84c

Please sign in to comment.