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

Form: reset form values outside of a Form component #6760

Open
damuso opened this issue Nov 10, 2024 · 2 comments
Open

Form: reset form values outside of a Form component #6760

damuso opened this issue Nov 10, 2024 · 2 comments
Labels
Resolution: Help Wanted Issue or pull request requires extra help and feedback

Comments

@damuso
Copy link

damuso commented Nov 10, 2024

Describe the bug

I am using the vue-query library for backend communication. After a successful request, I want to reset the form values. However, there is currently no way to access the reset function outside of the Form component because the $form variable (declared as v-scoper="$form") exists only within the Form component scope.

Upon reviewing the source code, I found a useForm function, similar to the one in vee-validate. However, since PrimeVue automatically registers all fields and defineField behaves differently, I cannot define $form outside the component using this function.

Example code:

<script setup lang="ts">

const formValues = ref({
  email: "",
});

const { isPending, mutate } = useMutation({
  mutationFn: (email: string) => apiRequest(email),
  onSuccess: () => {
    // TODO: how to reset form values? 
  }
});

const onSubmit = async ({ valid, values }: FormSubmitEvent) => {
  if (!valid || isPending.value ) return;
  mutate(values.email);
};
</script>

<template>
<Form
  v-slot="$form"
  :initial-values="formValues"
  @submit="onSubmit"
>
  <div class="flex flex-col gap-1">
    <FloatLabel>
      <InputText name="email" type="text" fluid />
      <label for="email">Email</label>
    </FloatLabel>
  </div>
</Form>
</template>

Reproducer

https://stackblitz.com/edit/primevue-4-ts-vite-issue-template-prtoxz

PrimeVue version

4.2.1

Vue version

3.x

Language

ALL

Build / Runtime

Nuxt

Browser(s)

No response

Steps to reproduce the behavior

No response

Expected behavior

No response

@damuso damuso added the Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible label Nov 10, 2024
@damuso damuso changed the title Form: reset form values outside of a component Form: reset form values outside of a Form component Nov 10, 2024
@jsodeman
Copy link

I would also like to see some of the Form functionality exposed to code, like .reset() , .validate(), .submit()

@jsodeman
Copy link

@tugcekucukoglu tugcekucukoglu added Resolution: Help Wanted Issue or pull request requires extra help and feedback and removed Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible labels Nov 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution: Help Wanted Issue or pull request requires extra help and feedback
Projects
None yet
Development

No branches or pull requests

3 participants