英文:
on conditional select input field branch is not a function
问题
我的模式:
export const experienceSchema = yup.object().shape({
experience: yup.string().required(),
job_title: yup.string().required(),
category: yup.string().required(),
current_working_status: yup.string().required(),
join_date: yup.date().required(),
end_date: yup
.date()
.notRequired()
.when('current_working_status', {
is: 'false',
then: yup.date().required('请输入日期'),
}),
});
我的HTML:
<div className="mt-2">
<label className="fw-medium">
当前是否在工作<sup>*</sup>
</label>
<div className="input-group">
<select
className="form-select"
id="select-example"
name="current_working_status"
onChange={formik.handleChange}
onBlur={formik.handleBlur}
value={formik.values.current_working_status}>
<option value="">选择一个选项</option>
<option value={true}>是</option>
<option value={false}>否</option>
</select>
</div>
{formik.touched.current_working_status && formik.errors.current_working_status ? <div className="edit_error">{formik.errors.current_working_status}</div> : null}
</div>
<div className="d-md-flex justify-content-between my-3">
<div className="col-md-5">
<label className="fw-medium">
入职日期<sup>*</sup>
</label>
<div className="input-group">
<input
type="date"
id="join_date"
name="join_date"
className="form-control"
placeholder="join_date"
onChange={formik.handleChange}
onBlur={formik.handleBlur}
value={formik.values.join_date}
/>
</div>
{formik.touched.join_date && formik.errors.join_date ? <div className="edit_error">{formik.errors.join_date}</div> : null}
</div>
{formik.values.current_working_status !== 'true' ? (
<div className="col-md-5">
<label className="fw-medium">结束日期</label>
<div className="input-group">
<input
type="date"
id="end_date"
name="end_date"
className="form-control"
placeholder="end_date"
onChange={formik.handleChange}
onBlur={formik.handleBlur}
value={formik.values.end_date}
/>
</div>
{formik.touched.end_date && formik.errors.end_date ? <div className="edit_error">{formik.errors.end_date}</div> : null}
</div>
) : (
''
)}
</div>
如果用户选择是当前在工作,我只想使用入职日期,如果选择否,我想同时使用入职日期和结束日期。反之亦然,我想进行验证。
当我选择否时,我遇到以下错误:
branch is not a function
TypeError: branch is not a function
请帮我解决这个问题。
我已经插入了条件验证的代码,但仍然无法解决它。
英文:
I am working on a application based on false i have to display end date.
my schema:
export const experienceSchema = yup.object().shape({
experience: yup.string().required(),
job_title: yup.string().required(),
category: yup.string().required(),
current_working_status: yup.string().required(),
join_date: yup.date().required(),
end_date: yup
.date()
.notRequired()
.when('current_working_status', {
is: 'false',
then: yup.date().required('Please enter date'),
}),
});
my html:
<div className="mt-2">
<label className="fw-medium">
Currently Working<sup>*</sup>
</label>
<div className="input-group">
<select
className="form-select"
id="select-example"
name="current_working_status"
onChange={formik.handleChange}
onBlur={formik.handleBlur}
value={formik.values.current_working_status}>
<option value="">Select an option</option>
<option value={true}>Yes</option>
<option value={false}>No</option>
</select>
</div>
{formik.touched.current_working_status && formik.errors.current_working_status ? <div className="edit_error">{formik.errors.current_working_status}</div> : null}
</div>
<div className="d-md-flex justify-content-between my-3">
<div className=" col-md-5">
<label className="fw-medium">
Joining Date<sup>*</sup>
</label>
<div className="input-group">
<input
type="date"
id="join_date"
name="join_date"
className="form-control"
placeholder="join_date"
onChange={formik.handleChange}
onBlur={formik.handleBlur}
value={formik.values.join_date}
/>
</div>
{formik.touched.join_date && formik.errors.join_date ? <div className="edit_error">{formik.errors.join_date}</div> : null}
</div>
{formik.values.current_working_status !== 'true' ? (
<div className=" col-md-5">
<label className="fw-medium">End Date</label>
<div className="input-group">
<input
type="date"
id="end_date"
name="end_date"
className="form-control"
placeholder="end_date"
onChange={formik.handleChange}
onBlur={formik.handleBlur}
value={formik.values.end_date}
/>
</div>
{formik.touched.end_date && formik.errors.end_date ? <div className="edit_error">{formik.errors.end_date}</div> : null}
</div>
) : (
''
)}
</div>
if user select currently working on yes, i just want to use join date, if select no i want use both join and end date. vice versa i want to validate.
When i select on no i am getting error as :
branch is not a function
TypeError: branch is not a function
Kindly please help me at this problem.
Using conditional validation i have inserted code still i am unable to work on it.
答案1
得分: 0
then
属性期望一个函数:
//...
end_date: yup.date().when("current_working_status", {
is: "false",
then: () => yup.date().required("请输入有效日期"),
otherwise: () => yup.date().notRequired()
}),
英文:
The then
property expects a function:
//...
end_date: yup.date().when("current_working_status", {
is: "false",
then: () => yup.date().required("Please enter a valid date"),
otherwise: () => yup.date().notRequired()
}),
答案2
得分: -1
要修复这个问题,您可以按以下方式修改您的模式定义:
export const experienceSchema = yup.object().shape({
experience: yup.string().required(),
job_title: yup.string().required(),
category: yup.string().required(),
current_working_status: yup.boolean().required(),
join_date: yup.date().required("请输入有效日期"),
end_date: yup.date().when("current_working_status", {
is: false,
then: yup.date().required("请输入有效日期"),
otherwise: yup.date().notRequired()
})
});
英文:
To fix this issue, you can modify your schema definition as follows:
export const experienceSchema = yup.object().shape({
experience: yup.string().required(),
job_title: yup.string().required(),
category: yup.string().required(),
current_working_status: yup.boolean().required(),
join_date: yup.date().required("Please enter a valid date"),
end_date: yup.date().when("current_working_status", {
is: false,
then: yup.date().required("Please enter a valid date"),
otherwise: yup.date().notRequired()
})
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论