gpt4 book ai didi

reactjs - Formik + 是的不显示错误

转载 作者:行者123 更新时间:2023-12-04 01:52:42 26 4
gpt4 key购买 nike

我有以下表格:

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com