-
Notifications
You must be signed in to change notification settings - Fork 932
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 require either one of two fields? (Cyclic dependency error) #79
Comments
Generally I move the validation up to the object. OR don't use yup.string().test(function (value) {
const { email } = this.parent;
if (!email) return value != null
return true
}) If you want to use |
Correction to the above link: https://github.com/jquense/yup/blob/master@{2017-01-20}/test/object.js#L609 |
You have a typo in the code. You are declaring a function and using an arrow afterwards. Corrected: yup.string().test(function (value) {
const { email } = this.parent;
if (!email) return value != null
return true
}) |
If you're wondering why this is not working, then make sure you're not using an arrow function for the callback |
var formModelSchema = yup.object({
email: yup.string().email().when('phone', {
is: (phone) => !phone || phone.length === 0,
then: yup.string().email().required(),
otherwise: yup.string()
}),
phone: yup.string().when('email', {
is: (email) => !email || email.length === 0,
then: yup.string().required(),
otherwise: yup.string()
})
},
// This is what you missed
[ [ 'email', 'phone' ] ]
); |
I had to use |
error |
Where is this documented? I would like to know how this works with properties of FieldArrays. |
The file format has beend changed to |
What (if possible) is the correct way to define that either the email or phone is a string > 1?
Uncaught (in promise) Error: Cyclic dependency: "phone"
The text was updated successfully, but these errors were encountered: