-
Notifications
You must be signed in to change notification settings - Fork 929
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to conditionally validate at least one of n values are set? #176
Comments
That's right but don't include the field itself there. That's why you are getting cycles, your telling yup the field depends on itself, which doesn't make sense |
That makes sense, however, I have fixed the issue and am still getting cyclic errors. Here's a reduced test case: https://runkit.com/nandastone/5a8ceae6d80f770012eb7270 Is yup running validation for sibling fields when they are referenced in |
Yeah this is a bit of a gotcha. Fields that depend on each other need to sorted so they are "constructed" in the right order, e.g. if depend on field There is an ugly escape hatch: using the |
I don't quite understand the low-level reason you've explained, but appreciate you taking the time to help. Your example worked well, until I extended the validation to depend on more than one other field: https://runkit.com/nandastone/5a8e0bf15ae96a0012e235a7 Thoughts? |
|
Has there been a resolution to this? I am experiencing the same thing. I upgraded the code a bit in one of the links to this and get
|
Hi @pmonty the issue in your last snippet I believe is because you aren't properly enumerating the list of 'edges' in your validation schema. Pretty much in the array of combinations at the end of the Anyway, I did this and it worked fine for me. |
I have an array of objects, and for each object, I have dependent fields. My code:
When I remove the The solutions here seem straightforward enough, so I'm sure I'm missing something small. Any help would be much appreciated. |
It turns out that |
@jmpolitzer Thanks, it saved me! |
This does not work anymore! Turns out that So the type of |
For now I got it work by changing the type to |
@loolooii, I ran into the same TypeScript issue, and it looks like you just have to upgrade @types/yup to v0.26.14 or later. The issue was fixed in DefinitelyTyped/DefinitelyTyped#35885. |
instead of using examples above, i just created a 'ghost' field in yup validation schema that checks specified fields, alot less code and no cyclic errors yup.object().shape({
a: yup.string(),
b: yup.string(),
AorB: yup.bool().when(['a', 'b'], {
is: (a, b) => (!a && !b) || (!!a && !!b),
then: yup.bool().required('some error msg'),
otherwise: yup.bool()
})
}) This will generate an error and prevent submit. When using Formik Fields just remember to include additional errors from this 'ghost' field, like: <Field
error={!!(errors && errors.a || (errors as any).AorB)}
helperText={errors && errors.b || (errors as any).AorB)}
/>
|
I only want to check
|
#176 (comment) This comment save my day ! |
Could you please provide me an example? I couldn't get it working :( |
This works for me: const phoneRegex = /^\+([0-9]{1,3})*\.([0-9]{5,16})$/;
yup.object().shape({
phoneNumber: yup.string()
.matches(phoneRegex)
.when('mobileNumber', {
is: val => !!val,
then: yup.string(),
otherwise: yup.string()required('phone or mobile number required')
}),
mobileNumber: yup.string()
.matches(phoneRegex)
.when('phoneNumber', {
is: val => !!val,
then: yup.string(),
otherwise: yup.string().required('phone or mobile number required')
})
}, [['mobileNumber', 'phoneNumber']]); |
this is working for me
|
it is throwing Uncaught Error: Cyclic dependency, the node was: "b". Someone, Please, help me with the working code. I required only one of the fields |
Is this still a problem or is there a documented solution? |
I'm trying something like
And getting |
I can follow what the author said but I think it's a horrible issue and should be mentioned in the document |
Hi All, Is this a correct way to mention your schema. I was getting the cyclic dependency issue too but the below schema resolved my issue.Just wanted to confirm if the below way of declaring the schema is correct.Please note that field field4 and field5 are not involved in the interdependent set of fields (1,2,3) schema = Yup.object().shape({ |
this worked... we need to add the cyclic dependent fields into the dependency of Yup object.. its more of like react hooks if i am not wrong.. |
how can we add type error message along with this? |
I'm having an issue getting this to work. I have 3 nested fields, and I want them to be optional, but if one of the the three is filled then they all need to be filled. Here's my code (the parent
I'm getting Can anyone point me in the right direction? |
I found the fix for my issue in #661 - I wasn't getting that you need to chain |
You can also use the test() function in yup validation to test whether the value is present or not in another dependent field. I tried in my case in which I have two dependent dropdown fields. Ex: In my case when I select a delivery_mode as a "Courier" then my Courier Name field is required and when I select the other delivery mode then it is a non-mandatory field. like this:
and I also add the condition in isRequired prop in the form for true or false like this: |
it all works fine with this approach but I cannot override the required in then with notRequired in otherwise. yup always return with the validation error for other fields if the condition is satisfied for one of the n values |
If I have 4 fields i.e. a, b, c, & d, then the pair-wise combination array at the end of
|
Awesome conditional checkbox validation code found here: jquense/yup#176 (comment)
…9876) * Refactor ConditionContent rendering There's two things that get done in this commit. 1. Add a "middle content" section that is conditionally displayed. This displays the "includes" text for all but the `daysWaiting` condition. As a result of this some css changes were made to correctly display this text. 2. Remove the getConditionContent() function and instead change that to a memoized conditionContent variable. I can't find an article on it but I was taught to avoid calling functions that render React components within jsx. I believe it caused rendering/state issues but idk I can't find anything on it so who knows. At the least this should have better performance using useMemo * Add DecisionReviewType condition for `ReportPage` For: https://jira.devops.va.gov/browse/APPEALS-30938 Add the DecisionReviewType condition field inputs for the Generate Task Report effort. * Upgrade scss_lint gem Goes from scss_lint (0.58.0) to scss_lint (0.60.0). * Add DecisionReviewType validation Awesome conditional checkbox validation code found here: jquense/yup#176 (comment) * Update margins of Conditions section This is to better match the designs. * Fix ConditionContainer useMemo call There was a useMemo call that didn't work as intended so it's now being removed. * Fix failing snapshot
I have 5 form fields, at last one of which must be populated for validation to succeed. I attempted this, but soon ran into cyclic reference issues:
What would be the best way to implement this?
The text was updated successfully, but these errors were encountered: