i was trying for 3 days now to get array validation working with Formik and Yup in React Native with a FieldArray. The Validation was working but i can't get it how to display the errors.
I have many tasks from the api where the user needs to write a comment if the switch is false, for testing purposes i disabled the switch to find out how to get the array validation running.
The TaskScreen:
import React, { useState } from 'react';
import { StyleSheet, Button } from 'react-native';
import * as yup from 'yup';
import { Formik } from 'formik';
import { SubmitButton, Tasks } from "../components/forms";
const validationSchema = yup.object().shape({
tasks: yup.array().of(yup.object().shape({comment: yup.string().required('Required'),})),
});
function TasksScreen({ navigation: { navigate }, route}) {
const [tasks, setTasks] = useState(route.params.tasks);
const handleSubmit = async (values) => {
console.log(values);
};
return (
<>
<Formik
initialValues={{ tasks: [] }}
onSubmit={handleSubmit}
validationSchema={validationSchema}
>
<Tasks name="tasks" tasks={tasks} />
<SubmitButton title="Send" />
</Formik>
<Button onPress={() => navigate('TaskLists') } title="Back"/>
</>
)
}
const styles = StyleSheet.create({
container: {},
});
export default TasksScreen;
The Tasks Component
import React from 'react';
import { useFormikContext, FieldArray } from "formik";
import ErrorMessage from "./ErrorMessage";
import { TextInput, Text, View, StyleSheet, Switch } from 'react-native';
function Tasks({ tasks, name }) {
const {
setFieldTouched,
setFieldValue,
errors,
touched,
values,
} = useFormikContext();
return (
<FieldArray name={name}>
<>
{ tasks.map((task, key)=>{
return (
<>
<View key={key}>
<Text>{task.name} {`${name}[${key}].comment]`}</Text>
<TextInput
onBlur={() => setFieldTouched(`${name}[${key}].comment`)}
onChangeText={(text) => setFieldValue(`${name}[${key}].comment`, text)}
value={values[`${name}[${key}].comment`]}
name={`${name}[${key}].comment`}
placeholder="Task comment please"
/>
<ErrorMessage error={`${errors}${name}[${key}].comment`} visible={`${touched}${name}[${key}].comment`} />
</View>
</>
)
})}
</>
</FieldArray>
);
}
const styles = StyleSheet.create({
container: {},
});
export default Tasks;
I also have problems to console.log the Formik Bag, can't figure it out, it's my first steps with RN, sorry if the question is boring.
Thanks in advance for any kind of help.