Comments (10)
@GalacticHypernova it's because you're using it in the wrong way. You can only pass string | Ref
. In your case sameAs compares to f1
as to a string value, but not as a reference to your form field.
In your particular example this is the only way for it to work (at least I didn't find any other):
const form = reactive({
f1: "",
f2: "",
});
const v$ = useVuelidate({
f1: { required },
f2: { sameAsF1: sameAs(computed(() => form.f1)) },
});
Not sure why toRef()
doesn't work, like below, to avoid using computed
in that case. But we have what we have.
f2: { sameAsF1: sameAs(toRef(form.f1)) },
Also, if you hardcode your value to be a ref
, but not a part of reactive form, it will also work. But that's definitely an invalid use-case...
const f2Hardcoded = ref('test')
const form = reactive({
f1: "",
f2: "",
});
const v$ = useVuelidate({
f1: { required },
f2: { sameAsF1: sameAs(f2Hardcoded) },
});
from vuelidate.
TLDR;
For those facing the same problem, especially if you're using composition API, here's the solution: just pass a computed function as a parameter for the sameAs function. Here is an example:
const form = reactive({
password: '',
password_confirmation: '',
email: '',
})
const rules = {
email: { email, required },
password: { required, minLength: minLength(3) },
password_confirmation: { required, sameAs: sameAs(computed(() => form.password)) },
}
const v$ = useVuelidate(rules, form)
from vuelidate.
Same issue. Is there any update? Thank you.
from vuelidate.
To resolve my issue, I just created a custom validation to check if the password is same to confirm password.
from vuelidate.
@m3c0d322 yea, the only solution is to use custom validation
from vuelidate.
Well, this does seem a little too overcomplicated with a bit too much boilerplate, and there's the very valid concern of data leaks when needing to create more reactive objects than necessary.. But thanks anyway for the workaround!
from vuelidate.
Well, I both agree and disagree :) Agreed, it looks a bit over-complicated, but at the same time, you need to do such manipulations quite rarely. And I don't really follow your concern about memory leaks – computed
is being passed a simple value here, so it will be released as soon as your component is released, so no tricks :)
from vuelidate.
Using Vue with Nuxt here.
The second I try to used a computed into sameAs the whole validation break with the following error
TypeError: v is undefined
My solution was to just do a custom validation:
sameAsPassword: helpers.withMessage(t('validations.sameAsPassword'), function (value: string) {
return value === state.password
}),
from vuelidate.
computed
is being passed a simple value here, so it will be released as soon as your component is released, so no tricks :)
I apologize for.the comically late response, I completely forgot about this. The reason I was talking about data leaks is because I have different uses for this, including something in an async context (after await). And I know some vue internals get funky when that happens, prime example being watchers not being auromatically stopped and unregistered, which would reult in a mem leak if you don't manually stop them. I'm not necessarily saying that it would for sure break anything, but I do not know how it would behave which could result in some peculiar edge cases.
from vuelidate.
My solution was to just do a custom validation
I also opted for a custom validation with a simple computed property. As it turns out, sometimes using the frameworks core features is the ideal option can save much of the abstraction overhead and complexity =)
from vuelidate.
Related Issues (20)
- Support ref (and possibly computed) HOT 2
- Not supporting this package in latest vue3 with typescript HOT 3
- InternalError: too much recursion: access validation states from a validation HOT 1
- How does i18n work when validators are chained?
- Validation inconsistent on Mobile Devices HOT 2
- Support Vue 3.x HOT 2
- a way to programatically set form/$model value without triggering validators
- root instance does not capture nested instances validations results if they have `$rewardEarly: true` and root does not.
- Getting path of validation error when validation fails in child component
- nuxt 3, $each helper does not work?
- $externalResults are not automatically cleared on $model change when using dynamic/computed rules HOT 1
- lazy validation option doesn't work HOT 1
- useVuelidate doesnt work in options API
- $rewardEarly stops working if a previous field is invalid HOT 1
- Vuelidate - foreach helper validation does not include $dirty property
- BUG: base-validation type is inferred to undefined
- Vuelidate not working when computed list is provided HOT 1
- Default error message of "required", "requiredIf" and "requiredUnless" should be equal
- Force Revalidation
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vuelidate.