GithubHelp home page GithubHelp logo

Comments (10)

letehaha avatar letehaha commented on June 11, 2024 2

@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.

joao-pedro-alves avatar joao-pedro-alves commented on June 11, 2024 1

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.

m3c0d322 avatar m3c0d322 commented on June 11, 2024

Same issue. Is there any update? Thank you.

from vuelidate.

m3c0d322 avatar m3c0d322 commented on June 11, 2024

To resolve my issue, I just created a custom validation to check if the password is same to confirm password.

from vuelidate.

GalacticHypernova avatar GalacticHypernova commented on June 11, 2024

@m3c0d322 yea, the only solution is to use custom validation

from vuelidate.

GalacticHypernova avatar GalacticHypernova commented on June 11, 2024

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.

letehaha avatar letehaha commented on June 11, 2024

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.

yuuzora avatar yuuzora commented on June 11, 2024

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.

GalacticHypernova avatar GalacticHypernova commented on June 11, 2024

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.

GalacticHypernova avatar GalacticHypernova commented on June 11, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.