You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, I have a form with many optional fields. I would like to validate these optional fields only when they are not empty. I tried something like this:
yup.object({
field1: yup.string().matches(/^([0-9]{10})$/, "Field 1 is not valid"),
field2: yup.string().min(5, "Field 2 is not valid")
});
These 2 fields are not required (there isn't "required" validation), but even if they are empty Yup tries to validate them anyway so it gives error because the empty string doesn't match with that regex (field1) and the empty string is less than 5 (field2).
How to validate them only when they are not empty? I tried to add the .notRequired() method, but it still doesn't work.
I know there might be these solutions:
Use the transform method and change the empty string in null then call the "nullable" method, like this:
field1: yup.string().matches(/^([0-9]{10})$/, "Field 1 is not valid").nullable().transform((value) => !!value ? value : null)
The issues with this solution are: this will also change the data value in the onSubmit function, the values will be null instead of empty strings but the API wants empty strings and not null values. The other problem (minor) is that I have many fields, so repeat that for every field is not elegant in my opinion.
Use the when method and put the validation in the then when the value is not empty, like this:
field2: yup.string().when({
is: (val) => val.length > 0,
then: yup.string().min(5, "Field 2 is not valid")
})
this solution works without issues, it doesn't change the value to null like transform, but as I said, I have many optional fields and I would like to avoid to repeat that for every field .
Is there a better way to do that? If the answer is no, then probably I will use the solution 2.
I'm using version 1.2.0
The text was updated successfully, but these errors were encountered:
Hi, I have a form with many optional fields. I would like to validate these optional fields only when they are not empty. I tried something like this:
These 2 fields are not required (there isn't "required" validation), but even if they are empty Yup tries to validate them anyway so it gives error because the empty string doesn't match with that regex (field1) and the empty string is less than 5 (field2).
How to validate them only when they are not empty? I tried to add the .notRequired() method, but it still doesn't work.
I know there might be these solutions:
transform
method and change the empty string in null then call the "nullable" method, like this:The issues with this solution are: this will also change the data value in the onSubmit function, the values will be null instead of empty strings but the API wants empty strings and not null values. The other problem (minor) is that I have many fields, so repeat that for every field is not elegant in my opinion.
when
method and put the validation in thethen
when the value is not empty, like this:this solution works without issues, it doesn't change the value to null like transform, but as I said, I have many optional fields and I would like to avoid to repeat that for every field .
Is there a better way to do that? If the answer is no, then probably I will use the solution 2.
I'm using version 1.2.0
The text was updated successfully, but these errors were encountered: