简体   繁体   English

是的,Formik 没有 Datepicker 值

[英]Yup with Formik doesn't Datepicker value

I have been trying everything to make this form work with yup, but when using the date-picker, the value doesn't register either within the field or with yup.我一直在尽一切努力使此表单与 yup 一起使用,但是当使用日期选择器时,该值既不会在字段中注册,也不会与 yup 一起注册。 My relevant imports are formik, yup, useForm and yupResolver.我的相关导入是 formik、yup、useForm 和 yupResolver。

I have removed non-relevant code to save space.我删除了不相关的代码以节省空间。 The other fields i have are first name, last name, email and phone number, and they all work as they should, with values registering and errors showing up when they don't fill the criteria.我拥有的其他字段是名字、姓氏、email 和电话号码,它们都按应有的方式工作,当它们不符合条件时会显示值注册和错误。

I have a console log withing the formik element which logs all the values, and all the values get logged properly except the dates, which stand empty.我有一个带有 formik 元素的控制台日志,它记录所有值,并且所有值都被正确记录,但日期除外,日期为空。

 const Form = () => { const [fromDate, setFromDate] = useState(new Date()); return ( <> <div> <Formik validationSchema={formVal}> {({ values, errors) => ( <form onSubmit={handleSubmit(onSubmit)}> <div> <DatePicker id="fromDate" onChange={(date) => { setFieldValue(date); }} /> </div> <Button type="submit" ></Button> </form> )} </Formik> </div> </> )}

It seems like you're not using 'setFieldValue' correctly;您似乎没有正确使用“setFieldValue”; The 1st argument should be the form's field name.第一个参数应该是表单的字段名称。

Should be something like this: setFieldValue('fromDate', date);应该是这样的: setFieldValue('fromDate', date); See https://formik.org/docs/api/formik for reference.请参阅https://formik.org/docs/api/formik以供参考。

Also, make sure that variable 'date' is of type Date and not an object, containing the actual date value.此外,请确保变量“日期”的类型为 Date,而不是包含实际日期值的 object。

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

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