简体   繁体   English

使用日期选择器验证对最终表单做出反应

[英]React final form with datepicker validation

Is there any way to display error message if the validation on the Field level fails when using react datepicker?如果在使用 react datepicker 时字段级别的验证失败,有什么方法可以显示错误消息?

I am using datepicker with react final form:我正在使用带有反应最终形式的日期选择器:

const DatePickerAdapter = ({ input: { onChange, value }, ...rest }) => (
  <DatePicker
    selected={value}
    onChange={(date) => {
      console.log(date);
      onChange(date);
    }}
    {...rest}
  />
);

And then I put into Field component from RFF:然后我从 RFF 放入 Field 组件:

      <Field
        name="date"
        validate={required}
        dateFormat="yyyy/MM/dd"
        component={DatePickerAdapter}
      />

The issue is that it wont display any error if for example I leave the field empty or any part of validation is not passing.问题是,例如,如果我将该字段留空或验证的任何部分未通过,它将不会显示任何错误。 Is there a way to display this error message somehow in final form component?有没有办法在最终表单组件中以某种方式显示此错误消息? Here is my codesandbox- other fields with validation can show error message but Field with datepicker is not: https://codesandbox.io/s/react-final-form-third-party-components-example-datepicker-forked-wzrqgr?file=/index.js:2392-2579这是我的代码框-其他带有验证的字段可以显示错误消息,但带有日期选择器的字段不是: https://codesandbox.io/s/react-final-form-third-party-components-example-datepicker-forked-wzrqgr?文件=/index.js:2392-2579

my recommendation would be to use Formik and Yup.我的建议是使用 Formik 和 Yup。 With Formik package use useFormik that will ask for initial values that would be empty string, validationSchema that would be from yup and lastly ask for onSubmit that is linked to button.使用 Formik package 使用 useFormik,它将要求初始值是空字符串,validationSchema 将来自是的,最后要求与按钮链接的 onSubmit。

See a quick example in this url:请参阅此 url 中的一个简单示例:

https://stackblitz.com/edit/react-ff8inu?file=src/App.js https://stackblitz.com/edit/react-ff8inu?file=src/App.js

This code will not work since the program is wrote inside Stackblitz react and not react native but idea stay the same此代码将不起作用,因为程序是在 Stackblitz 内部编写的,反应而不是本机反应,但想法保持不变

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

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