gpt4 book ai didi

reactjs - React Formik + 是的,onChange 触摸了字段

转载 作者:行者123 更新时间:2023-12-03 13:35:04 24 4
gpt4 key购买 nike

我想有条件地在表单中显示错误。

formik 的工作方式是,如果您更改一个字段,即使您只更改了一个字段,也会运行所有验证并返回所有错误。

我想仅在字段被触摸时显示错误,并且我希望在更改时触摸字段。对该字段的第一次更改应该会使其受到影响。

目前,formik 正在提交时触摸字段。我怎样才能在更改时触摸它?

这是我当前的形式:

const optionsForSelect = (collection) => {
return collection.map(item => ({
value: item.id,
label: item.name
}))
}

const validationSchema = yup.object().shape({
length: yup
.number()
.min(1, 'Length should be a positive non-zero integer')
.required(),
frame_rate: yup
.string()
.required()
})

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

const debouncedHandleChange = debounce(handleChange, 200)

console.log(errors)
console.log('TOUCHED')
console.log(touched)
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={ (e) => handleInputChange(e, debouncedHandleChange) }
formName='turbosquid_product_form_length'
fieldName='length'
/>

<div className="form-field-error">{errors.length ? errors.length : "No Error"}</div>

<PanelSelectField
label='Frame Rate'
value={ values.frame_rate }
onChange={ ({value}) => handleSelectChange('frame_rate', value) }
formName='turbosquid_product_form_frame_rate'
fieldName='frame_rate'
options={ optionsForSelect(frameRateDropdownData) }
searchable={ false }
clearable={ false }
/>
</div>

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

<PanelCheckBox
label='Biped'
checked={ values.biped }
onChange={ (e) => handleInputChange(e, debouncedHandleChange) }
fieldName='biped'
formName='turbosquid_product_form_biped'
/>

<PanelCheckBox
label='Loopable'
checked={ values.loopable }
onChange={ (e) => handleInputChange(e, debouncedHandleChange) }
fieldName='loopable'
formName='turbosquid_product_form_loopable'
/>
</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 字段,您可以执行以下操作:

<Formik
initialValues={initialValues}
onSubmit={(values) => {
//submit form
}}>
{({ setFieldTouched, handleChange }) => {
return (
<Form>
<Field
name="type"
onChange={e => {
setFieldTouched('type');
handleChange(e);
}} />
</Form>
)
}}

关于reactjs - React Formik + 是的,onChange 触摸了字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52258083/

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