Skip to content
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

Cant access field methods or props from ref #3443

Closed
gaby64 opened this issue Aug 13, 2021 · 7 comments
Closed

Cant access field methods or props from ref #3443

gaby64 opened this issue Aug 13, 2021 · 7 comments

Comments

@gaby64
Copy link

gaby64 commented Aug 13, 2021

I cant access field methods or meta from ref

I would like to be able to call resetField from a method.

@logaretm
Copy link
Owner

It's added in 9f96ef0 and should tagged in the next 4.5-alpha release or 4.5 full

the added methods are:

  • setErrors
  • setTouched
  • reset
  • validate
  • handleChange

@gaby64
Copy link
Author

gaby64 commented Aug 13, 2021

could meta be also exposed?

@logaretm
Copy link
Owner

I don't think exposing state via $ref makes sense, so only methods will be exposed just like the Form component. As a workaround, you can use v-slot instead or go with the composition API.

@gaby64
Copy link
Author

gaby64 commented Aug 16, 2021

is there a performance cost to exposing it?

at the moment, im using the v-slot and passing meta as a function argument.
I dont see why accessing it from ref would be an issue, especially if I want to be able to use the state from a function not called by an event handler in the template.

I am not yet ready to go the composition API route.

@logaretm
Copy link
Owner

I dont see why accessing it from ref would be an issue

Because it would encourage using $refs more often which is not a good practice.

Another workaround you can use is to copy the code for the <Field /> component into your project and expose whatever you need and use that instead of vee-validate's <Field /> and it should work fine with the <Form /> component and other APIs.

@lk77
Copy link

lk77 commented Oct 28, 2021

@logaretm Is there a way to access valid states outside of template / component containing Form?

Submit is triggered programmatically, not via a submit button inside the form,
because form is displayed on a dialog component with already embedded buttons, i could include the buttons in my form but i prefer to let the dialog component handle that for me.

I need to pass the meta.valid to the parent dialog component

i don't see a way to do that for now, i could call validate(), but this will trigger validation visually, and display errors to the users.

for now i'm doing that :

<Form v-slot="{ meta }">
    <input v-bind="meta" type="hidden" ref="meta">
</Form>
methods: {
    isValid() {
        return this.$refs.meta.getAttribute('valid')
    }
}

that's not clean at all but i have no choice

@ghost
Copy link

ghost commented Dec 21, 2021

@logaretm What do you think about implementing the composable in such a way as to play friendly with the <Form> component? A valid use case would be attaching form actions to an external action bar without too much headache.

<Form name="myForm">
const { meta } = useForm({ name: "myForm" })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants