在条件选择输入字段分支上,”branch is not a function”。

huangapple go评论79阅读模式
英文:

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(&#39;current_working_status&#39;, {
            is: &#39;false&#39;,
            then: yup.date().required(&#39;Please enter date&#39;),
        }),
});

my html:

&lt;div className=&quot;mt-2&quot;&gt;
    &lt;label className=&quot;fw-medium&quot;&gt;
        Currently Working&lt;sup&gt;*&lt;/sup&gt;
    &lt;/label&gt;
    &lt;div className=&quot;input-group&quot;&gt;
        &lt;select
            className=&quot;form-select&quot;
            id=&quot;select-example&quot;
            name=&quot;current_working_status&quot;
            onChange={formik.handleChange}
            onBlur={formik.handleBlur}
            value={formik.values.current_working_status}&gt;
            &lt;option value=&quot;&quot;&gt;Select an option&lt;/option&gt;
            &lt;option value={true}&gt;Yes&lt;/option&gt;
            &lt;option value={false}&gt;No&lt;/option&gt;
        &lt;/select&gt;
    &lt;/div&gt;
    {formik.touched.current_working_status &amp;&amp; formik.errors.current_working_status ? &lt;div className=&quot;edit_error&quot;&gt;{formik.errors.current_working_status}&lt;/div&gt; : null}
&lt;/div&gt;
&lt;div className=&quot;d-md-flex justify-content-between my-3&quot;&gt;
&lt;div className=&quot; col-md-5&quot;&gt;
&lt;label className=&quot;fw-medium&quot;&gt;
Joining Date&lt;sup&gt;*&lt;/sup&gt;
&lt;/label&gt;
&lt;div className=&quot;input-group&quot;&gt;
&lt;input
type=&quot;date&quot;
id=&quot;join_date&quot;
name=&quot;join_date&quot;
className=&quot;form-control&quot;
placeholder=&quot;join_date&quot;
onChange={formik.handleChange}
onBlur={formik.handleBlur}
value={formik.values.join_date}
/&gt;
&lt;/div&gt;
{formik.touched.join_date &amp;&amp; formik.errors.join_date ? &lt;div className=&quot;edit_error&quot;&gt;{formik.errors.join_date}&lt;/div&gt; : null}
&lt;/div&gt;
{formik.values.current_working_status !== &#39;true&#39; ? (
&lt;div className=&quot; col-md-5&quot;&gt;
&lt;label className=&quot;fw-medium&quot;&gt;End Date&lt;/label&gt;
&lt;div className=&quot;input-group&quot;&gt;
&lt;input
type=&quot;date&quot;
id=&quot;end_date&quot;
name=&quot;end_date&quot;
className=&quot;form-control&quot;
placeholder=&quot;end_date&quot;
onChange={formik.handleChange}
onBlur={formik.handleBlur}
value={formik.values.end_date}
/&gt;
&lt;/div&gt;
{formik.touched.end_date &amp;&amp; formik.errors.end_date ? &lt;div className=&quot;edit_error&quot;&gt;{formik.errors.end_date}&lt;/div&gt; : null}
&lt;/div&gt;
) : (
&#39;&#39;
)}
&lt;/div&gt;

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(&quot;current_working_status&quot;, {
  is: &quot;false&quot;,
  then: () =&gt; yup.date().required(&quot;Please enter a valid date&quot;),
  otherwise: () =&gt; 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(&quot;Please enter a valid date&quot;),
end_date: yup.date().when(&quot;current_working_status&quot;, {
is: false,
then: yup.date().required(&quot;Please enter a valid date&quot;),
otherwise: yup.date().notRequired()
})
});

huangapple
  • 本文由 发表于 2023年7月17日 21:27:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76704956.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定