Tengo dos matrices y quiero que al menos una de ellas se llene antes de enviar
Mi configuración:
initialValues={{ images: [], current_images: [], }} validationSchema={Yup.object().shape( { images: Yup.array().when("current_images", { is: (current_images) => current_images.length === 0, then: Yup.array().min(1, "Pick at least one image"), otherwise: Yup.array(), }), current_images: Yup.array().when("images", { is: (images) => images.length === 0, then: Yup.array().min(1, "Pick at least one image"), otherwise: Yup.array(), }), }, [["current_images", "images"]] )}
Versión:
"formik": "^2.2.9", "yup": "^0.32.11"
Aquí está el error que recibí después de hacer clic en el botón Enviar:
Como puedo solucionar este problema ?
Encontré una solución alternativa que usa .test()
en lugar de .when()
initialValues={{ images: [], current_images: [], }} validationSchema={Yup.object().shape( { images: Yup.array().test( "images_required", // test name "Pick at lease one image", // error message function (item) { // item is the current field (images in this case) return item.length > 0 || this.parent.current_images.length > 0; } }), current_images: Yup.array().test( "current_images_required", "Pick at lease one image", function (item) { return item.length > 0 || this.parent.images.length > 0; } }), } )}