admin管理员组文章数量:1426506
I want to create dynamic Form using Formik and Yup. On plus button press, new inputs should be added. However when I create validation schema, onSubmit method is not calling. When I delete validationSchema I can see the log in my console.
Here is my code:
interface Props {
data?: string;
onSubmit?: Function
}
interface IFormValues {
people: {name: string, surname: string}[]
}
const FieldComponent = ({field, form: { touched, errors }}) => {
const error = getIn(errors, field.name);
const touch = getIn(touched, field.name);
return (
<div>
<input type="text" name={field.name} onChange={field.onChange}/>
{touch && error ? <p>{error}</p> : null}
</div>
)
}
const FieldArrayComponent = (arrayHelpers) => (
<div>
{arrayHelpers.form.values.people.map((person, index) => (
<div key={index}>
<Field name={`people.${index}.name`} ponent={FieldComponent}/>
<Field name={`people.${index}.surname`} ponent={FieldComponent}/>
<button type="button" onClick={() => arrayHelpers.push({name: '', surname: ''})}>+</button>
<button type="button" onClick={() => arrayHelpers.remove(index)}>-</button>
</div>
))}
<div>
<button type="submit">Submit</button>
</div>
</div>
)
export const FormComponent: React.FunctionComponent<Props> = (props) => {
const initialValues: IFormValues = {
people: [{name: '', surname: ''}]
}
const schema = yup.object().shape({
people: yup.array().of(
yup.object().shape({
name: yup.string().required('Required'),
surname: yup.string().required('Required')
})
)
})
return (
<Formik
initialValues={initialValues}
onSubmit={values => {
console.log(values);
}}
validationSchema={schema}
render={({ values }) => (
<Form>
<FieldArray
name="people"
ponent={FieldArrayComponent}
/>
</Form>
)}
/>
)
}
Can you take a look what I am doing wrong?
I want to create dynamic Form using Formik and Yup. On plus button press, new inputs should be added. However when I create validation schema, onSubmit method is not calling. When I delete validationSchema I can see the log in my console.
Here is my code:
interface Props {
data?: string;
onSubmit?: Function
}
interface IFormValues {
people: {name: string, surname: string}[]
}
const FieldComponent = ({field, form: { touched, errors }}) => {
const error = getIn(errors, field.name);
const touch = getIn(touched, field.name);
return (
<div>
<input type="text" name={field.name} onChange={field.onChange}/>
{touch && error ? <p>{error}</p> : null}
</div>
)
}
const FieldArrayComponent = (arrayHelpers) => (
<div>
{arrayHelpers.form.values.people.map((person, index) => (
<div key={index}>
<Field name={`people.${index}.name`} ponent={FieldComponent}/>
<Field name={`people.${index}.surname`} ponent={FieldComponent}/>
<button type="button" onClick={() => arrayHelpers.push({name: '', surname: ''})}>+</button>
<button type="button" onClick={() => arrayHelpers.remove(index)}>-</button>
</div>
))}
<div>
<button type="submit">Submit</button>
</div>
</div>
)
export const FormComponent: React.FunctionComponent<Props> = (props) => {
const initialValues: IFormValues = {
people: [{name: '', surname: ''}]
}
const schema = yup.object().shape({
people: yup.array().of(
yup.object().shape({
name: yup.string().required('Required'),
surname: yup.string().required('Required')
})
)
})
return (
<Formik
initialValues={initialValues}
onSubmit={values => {
console.log(values);
}}
validationSchema={schema}
render={({ values }) => (
<Form>
<FieldArray
name="people"
ponent={FieldArrayComponent}
/>
</Form>
)}
/>
)
}
Can you take a look what I am doing wrong?
Share Improve this question asked Jul 19, 2019 at 14:21 hetioushetious 8031 gold badge9 silver badges14 bronze badges1 Answer
Reset to default 6The purpose of passing validationSchema
is to ensure that onSubmit
doesn't get called if there are errors
. I created a working example from your code. Have a look: https://stackblitz./edit/demo-react-formik-validation-schema
You can see that onSubmit
does get called if there are no errors. But if required fields are empty, onSubmit
does not get called. That is the expected behavior.
However, if your intention was to debug in the case when errors are there (which is what I need to do quite often), checkout render
method of Formik
in which you can console.log(values, errors)
to see the form errors or values instead of logging in onSubmit
.
render = {({ values, errors }) => {
console.log(values, errors);
return <Form>
<FieldArray
name="people"
ponent={FieldArrayComponent}
/>
</Form>
}}
本文标签: javascriptForm submission does not work with validationSchemaStack Overflow
版权声明:本文标题:javascript - Form submission does not work with validationSchema - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745469047a2659664.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论