Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Custom Validations for FieldArray fields #4995

Open
emiltg0045 opened this issue Feb 26, 2025 · 0 comments
Open

Support Custom Validations for FieldArray fields #4995

emiltg0045 opened this issue Feb 26, 2025 · 0 comments

Comments

@emiltg0045
Copy link

emiltg0045 commented Feb 26, 2025

As of this moment, custom validator functions do not work with field arrays.

const validationSchema = {
    'fieldArray.*': validatorFn
}

const { meta, errors, setErrors, handleSubmit, setFieldValue } = useForm({
    validationSchema
})

The upper passage does not work.

I tried to work around this by just returning an array of [null, null, 'Error here!'], but the errors property returned by useForm() returns the first element/does not return the array at all:

function validateFieldArray(value) {
        let result = []

        for (let i = 0; i < value.length; i++) {
            if (!value[i].trim().length) {
                result[i] = 'This field is required!'
            }
        }

       console.log(result) // [empty, empty, 'This field is required!']

        // returning true means no error
        return !result.length ? true : result
}

const validationSchema = {
    fieldArray: validateFieldArray,
}

// errors object comes back empty {}

Requested Solution: implement support for custom validator functions (no yup/zod) for field arrays, or let errors object pass arrays as error messages

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant