-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Matching fields example (ie. Passwords) #90
Comments
Yup doesn't make this common usecase as easy as it should. Here's how I've gotten this to work (a slight modification of code in jquense/yup#97)
... then in your validationSchema:
|
Thanks so much for this. I see how it's more complicated than I had originally thought, although this all makes sense. Appreciate you taking the time. |
For me that is not working. Did anything regarding @eonwhite I'm using your code, but I don't get the link. I already searched for that issue but I didn't find any working solution to get the actual value of the ref. *edit: I just saw this issue is posted for formik, so probably not the best place to answer if anything changed. But maybe someone has an idea though. (I came from jquense/yup#97) |
FYI folks this has gotten easier in the most recent version of yup. validationSchema: Yup.object({
password: Yup.string().required('Password is required'),
passwordConfirm: Yup.string()
.oneOf([Yup.ref('password'), null])
.required('Password confirm is required')
})
`` |
@jquense In 0.23.0? Doesn't work for me with
|
@jquense I found that the validation is incorrectly showing passwords that do in fact match as an error. |
@FahdW I have a custom error set. I just posted the minimal implementation. |
@vuhrmeister Yeah the oneOf is not working i had to add my own method to get it to work |
looks like a bug, 'ill take a look |
@jquense Doesn't work for my either, please take a look |
tried mixed().oneOf ... string().oneOf ... nothing seems to work to match 2 fields :( |
works with [email protected] |
Following @eonwhite solution is more correct. |
@janzenz just specify custom error message .oneOf([ref('password')], 'Passwords do not match') |
This doesn't work if I want to allow the password field to be empty. password: Yup.lazy(
value =>
!value
? Yup.string()
: Yup.string()
.min(6, 'Password must be at least 6 characters')
.required('Password is required'),
),
confirmPassword: Yup.string().oneOf(
[Yup.ref('password')],
'Passwords do not match',
), The schema returns valid if |
I found that there is no 'equalTo' method in yup. This is a convenient method needed by everyone. yup.addMethod(yup.mixed, 'equalTo', function(ref, message) {
const msg = message || '${path} should match ${ref.path}';
return this.test('equalTo', msg, function (value) {
let refValue = this.resolve(ref);
return !refValue || !value || value === refValue;
})
}) |
I used manually triggering validation. Added validate attribute to the password confirmation Field: ...
<Field type="password" name="password" id="password" />
<Field type="password" name="password_confirm" id="password_confirm" validate={validatePassword} />
... and validatePassword function: const validatePassword = (password) => {
let error
if (!password) {
error = "Confirm password"
}
else if (password !== document.getElementById("password").value) {
error = "Passwords do not match"
}
return error
} |
I just wanted to point out that if you want to display a custom error message based on @jquense 's answer, you'd do it like this:
|
Just in case some has this problem. |
If anyone is attempting to validate a |
just add validate to Fomatik tag,it works like a charm.here is a working sample: const schema = yup.object({
<Formik
|
If anyone is looking for a solution to validate that two fields are the same ONLY when the first one is filled in then here it is. The use-case here is a naive update profile page which only wants to validate the password is being changed if you fill in the field:
As you can see the |
This works for me for a simple password confirmation
|
any advancement made? nothing above mentioned work for me...! |
@IZUNA894 Hey, does this help vvv ?
|
Just going to put this out there, but why not something like: const schema = yup.object({
password: yup.string().required(),
repeatPassword: yup.string().matches(objectToTest.password).required()
}) |
This schema works well if you want to validate that both passwords match and then show the green checkmarks to show that they both match: Validation Schema:
Show Green Checkmarks in Password Fields if they Both Match:Add this to each of the password fields' props to make the green checkmarks appear if both passwords match (make sure to include In the
... In the
|
I'm struggling with getting a Yup validation working to ensure two fields match (in this case, password and password confirm).
I'm new to Formik & Yup (and relatively new to JS generally). Really appreciate any pointers.
The text was updated successfully, but these errors were encountered: