reactjs - Formik + 是的不显示错误

标签 reactjs formik yup

我有以下表格:

import React from 'react'
import PanelInputField from './form_components/panel_input_field'
import * as yup from 'yup'
import { withFormik, FormikErrors, FormikProps } from "formik";

const validationSchema = yup.object().shape({
  length: yup
    .number()
    .min(200, 'NOT BIG ENOUGH')
    .required()
})

class SpecificationsForm extends React.PureComponent {
  render() {
    const {
      values,
      handleInputChange,
      handleSelectChange,
      touched,
      errors
    } = this.props;

    console.log(errors)
    return (
      <div className="panel panel-default specifications-panel" id="js-turbosquid-product-specifications-panel">
        <div className="panel-heading">
          <a href="#" className="js-more-info" data-toggle="collapse" data-target="#specifications-panel-instructions" tabIndex="-1">
            Specifications
            <i className="fa fa-question-circle" />
          </a>
        </div>

        <div className="panel-body panel-collapse collapse in" id="specification-panel-body">
          <div className="panel-body-container">
            <div id="specifications-panel-instructions" className="panel-instructions collapse" />

            <div className="row">
              <div className="col-xs-6">

                <PanelInputField 
                  label='Length'
                  value={ values.length }
                  onChange={ handleInputChange } 
                  formName='turbosquid_product_form_length'
                  fieldName='length'
                />
                <div className="form-field-error">{errors.length ? errors.length : "No Error"}</div>
            </div>
          </div>
        </div>
      </div>
    )
  }
}

const ProductSpecificationsMotionCapturePanel = withFormik({
  validationSchema,
  enableReinitialize: true,
  mapPropsToValues: (props) => (props),
  handleInputChange: (props) => (props.handleInputChange),
  handleSelectChange: (props) => (props.handleSelectChange),
})(SpecificationsForm)

export default ProductSpecificationsMotionCapturePanel

表单工作正常,它显示出来,但我似乎无法让 formik 看到错误。在这个例子中,我有一个带有整数值的长度字段。每当我输入内容时,验证都会起作用(打印 console.log)但对象完全为空。

即使我在输入中写入非数字,也不会显示任何错误。

有没有人对我可以做些什么来使它工作有什么建议?

最佳答案

您是否设置了初始值?看起来你没有在这个文件中。如果没有,请尝试设置它,看看它是否有效。

关于reactjs - Formik + 是的不显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52204698/

相关文章:

javascript - reactjs 语法错误 : url is readonly

javascript - react : state not updating on first click

reactjs - 如何使用react-input-mask( typescript )屏蔽formic中的 Material ui输入

reactjs - Material-UI AutoComplete 表单中的 freeSolo

javascript - 如果在 yup Schema 验证中任何字段非空,则不需要任何字段或需要所有字段

javascript - 使用 Yup 和 Formik 进行条件验证

javascript - 使用 Yup 和 Typescript 的动态验证消息

ios - 如何在React Native(iOS)中打开URL文件类而不是Bundle文件类

javascript - 收到错误 : `ReferenceError: Cannot access ' imported const' before initialization` in react

javascript - 如何使用 formik react js 在 handlesubmit 函数中接收选择值?