question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

how to validate that a field is the same as another?

See original GitHub issue

How can I have rules to validate that the user wrote the same account and confirmedAccount?

const inst = yup.object({
  account: yup.string().min(7).required(),
  confirmedAccount: yup.string().oneOf([yup.ref('account')])
 });
inst.validate({ account: '123456', confirmedAccount: '234' }, {
	abortEarly: false
}).then(function() { 
	console.log('ok', arguments);
})
.catch(function(error) {
	console.log('failed', error);
});

console.log(inst.cast({ account: '123456', confirmedAccount: '234' }));

Looks like yup.ref only works for casting data, not for validations? So, how can I validate it?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:18 (4 by maintainers)

github_iconTop GitHub Comments

78reactions
csantos1113commented, Jun 6, 2017

FYI: I created this extended validation that looks working:

function equalTo(ref, msg) {
	return this.test({
		name: 'equalTo',
		exclusive: false,
    message: msg || '${path} must be the same as ${reference}',
		params: {
			reference: ref.path
		},
		test: function(value) {
      return value === this.resolve(ref) 
		}
	})
};

yup.addMethod(yup.string, 'equalTo', equalTo);


 const inst = yup.object({
   account: yup.string().min(7).required(),
   confirmedAccount: yup.string().equalTo(yup.ref('account'))
 });

inst.validate({ account: '1234567', confirmedAccount: '12345678' }, {
	abortEarly: false
}).then(function() { 
	console.log('ok', arguments);
})
.catch(function(error) {
	console.log('failed', error);
})
69reactions
Yaojiancommented, Dec 27, 2018

https://github.com/jquense/yup/issues/49#issuecomment-393100970 has a solution:

password: yup.string().required('Password is required'),
confirmPassword: yup.string().oneOf([yup.ref('password'), null], "Passwords don't match").required('Confirm Password is required'),
Read more comments on GitHub >

github_iconTop Results From Across the Web

Yup validation with two fields related - Stack Overflow
Validation using value of other form field using Yup test(). · 3. Try, when answering, write code with its description · (val) =>...
Read more >
Cross Field Validation - VeeValidate
To reference another field's value, add a @ at the beginning of a param to signal to vee-validate that it should substitute the...
Read more >
Laravel validate a field to some value if another ... - Laracasts
Laravel validate a field to some value if another field is different from the same value ? Hello guys,. in my form i...
Read more >
How can I ensure that two fields have the same value?
Add an Email field labeled “Email” and another Email field labeled “Confirm Email”. Select the Confirm Email field and find the Show Custom...
Read more >
Cross Field Validation Using Angular Reactive Forms
This powerful features let you validate not a single form control but instead you can validate one form control against the value from...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found