简体   繁体   English

使用 Formik 和 Yup 在 React 中未提交表单

[英]Form not Submitting in React using Formik and Yup

I'm using Autocomplete component in Material UI and I'm using Formik and Yup for validations.我在 Material UI 中使用Autocomplete组件,并且使用FormikYup进行验证。 My problem is that when I submit my values and console it.我的问题是,当我提交我的值并对其进行控制台时。 It doesn't appear which means it doesn't conform to its yup validation.它没有出现,这意味着它不符合其 yup 验证。

Pls check my codesandbox here Click here请在此处检查我的代码和框 单击此处

<Formik
  validationSchema={citySchema}
  initialValues={{ city_id: [{ id: null, name: "" }] }}
  onSubmit={submit}
>
  {({ handleChange, values, setFieldValue, touched, errors }) => (
    <Form>
      <Autocomplete
        id="city_id"
        name="city_id"
        options={cities}
        getOptionLabel={(option) => option.name}
        style={{ width: 300 }}
        onChange={(e, value) => {
          setFieldValue("city_id", value);
        }}
        renderInput={(params) => (
          <TextField
            label="City"
            variant="outlined"
            helperText={touched.city_id && errors.city_id}
            error={touched.city_id && Boolean(errors.city_id)}
            {...params}
          />
        )}
      />
      <Button variant="contained" color="primary" type="submit">
        Submit
      </Button>
    </Form>
  )}
</Formik>

Your validationSchema requires a name but your code never supplies it.您的validationSchema需要一个name ,但您的代码从不提供它。 Either make it optional or add a name input field.使其成为可选或添加name输入字段。

An easy way to debug this is to print out the errors formik is supplying you:一种简单的调试方法是打印出 formik 提供给您的错误:

 <Form>
          <pre>{JSON.stringify(errors, null, 2)}</pre>
          <Autocomplete
            ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM