简体   繁体   English

Formik +是不显示错误

[英]Formik + yup not displaying errors

I have the following form: 我有以下表格:

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

The form works properly, it is displayed and everything but I can't seem to make formik see the errors. 表单可以正常工作,可以显示所有内容,但是我似乎无法让formik看到错误。 In this example I have the length field with integer values. 在此示例中,我具有带有整数值的长度字段。 Whenever I input something, the validations work (the console.log prints) but the object is totally empty. 每当我输入内容时,验证就可以工作( console.log打印),但是对象完全为空。

Even if I write non numbers in the input, no errors are displayed ever. 即使我在输入中写上非数字,也不会显示任何错误。

Does anyone have a suggestion on what I could do to make it work? 有人对我可以做些什么有建议吗?

Did you setup initial values? 您是否设置了初始值? It looks like you did not in this file. 看来您不在此文件中。 If not, try setting that up and see if it works. 如果没有,请尝试进行设置,看看是否可行。

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

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