gpt4 book ai didi

javascript - Formik 和 yup 表单验证无法按预期使用 Virtualized Select

转载 作者:数据小太阳 更新时间:2023-10-29 06:04:36 32 4
gpt4 key购买 nike

为了进行表单验证,我使用 formik 创建了一个表单。我已经使用了组件 Formik、Form、Field form formik 并配置了它们:

    import { Formik, Form, Field } from "formik";
import { object, string } from "yup";
import isEmpty from "lodash/isEmpty";
import FormikSelectInput from "../common/FormikSelectInput";

class App extends Component {
render() {
const options = this.props.categories.map(c => {
return { label: c.name, value: c.name };
});

return (
<Formik
validationSchema={object().shape({
category: string().required("Category is required.")
})}
initialValues={this.props.obj}
onSubmit={(values, actions) => {
console.log(values);
}}
render={({ errors, dirty, isSubmitting, setFieldValue }) => (
<Form>
<Field
name="category"
label="Categories"
value={this.props.obj.category.name}
options={options}
component={FormikSelectInput}
/>
<button
type="submit"
className="btn btn-default"
disabled={isSubmitting || !isEmpty(errors) || !dirty}
>
Save
</button>
</Form>
)}
/>
);
}
}

//Prop Types validation
App.propTypes = {
obj: PropTypes.object.isRequired,
categories: PropTypes.array.isRequired,
actions: PropTypes.object.isRequired
};
const getElementByID = (items, id) => {
let res = items.filter(l => l.id === id);
return res.length ? res[0] : null; //since filter returns an array we need to check for res.length
};
//Redux connect
const mapStateToProps = ({ objects, categories }, ownProps) => {
let obj = {
id: "",
name: "",
category: { id: "", name: "" }
};
return {
obj: getElementByID(objects, ownProps.match.params.id) || obj,
categories: categories
};
};

export default connect(
mapStateToProps,
{...}
)(App);

我有一个自定义组件“FormikSelectInput”:

import React, { Component } from "react";
import classnames from "classnames";
import VirtualizedSelect from "react-virtualized-select";
import "react-select/dist/react-select.css";
import "react-virtualized/styles.css";
import "react-virtualized-select/styles.css";

const InputFeedback = ({ children }) => (
<span className="text-danger">{children}</span>
);

const Label = ({ error, children, ...props }) => {
return <label {...props}>{children}</label>;
};

class FormikSelectInput extends Component {
constructor(props) {
super(props);
this.state = { selectValue: this.props.value };
}

render() {
const {
field: { name, ...field }, // { name, value, onChange, onBlur }
form: { touched, errors }, // also values, setXXXX, handleXXXX, dirty, isValid, status, etc.
className,
label,
...props
} = this.props;

const error = errors[name];
const touch = touched[name];
const classes = classnames(
"form-group",
{
"animated shake error": !!error
},
className
);

console.log("props", props);
return (
<div className={classes}>
<Label htmlFor={name} error={error}>
{label}
</Label>
<VirtualizedSelect
name={name}
id={name}
className="form-control"
{...field}
{...props}
onChange={(selectValue) => this.setState(() => {
this.props.form.setFieldValue('category',selectValue)
return { selectValue }
})}
value={this.state.selectValue}
/>
{touch && error && <InputFeedback>{error}</InputFeedback>}
</div>
);
}
}

export default FormikSelectInput;

我的组件正在工作,我可以选择一个选项,但是为什么当我清空选择字段时 formik 和“yup”验证向我显示错误。

当我清除我的选择字段时,我得到一个错误 - 'category must be a string type, but the final value was: null。如果“null”意为空值,请务必将架构标记为 .nullable()'

enter image description here

我的代码基于 this example .

最佳答案

根据您的 validationSchema,该字段似乎需要字符串。

这个错误帮助我指明了正确的方向。这是 Yup .nullable() 的文档:https://github.com/jquense/yup#mixednullableisnullable-boolean--true-schema

尝试将 .nullable() 添加到验证链中。

validationSchema={object().shape({
category: string().required("需要类别。").nullable()
})}

希望这对您有所帮助。 enter image description here

关于javascript - Formik 和 yup 表单验证无法按预期使用 Virtualized Select,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51222559/

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