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

@primevue/forms: Form components submit event parameter missing values #7006

Closed
root5427 opened this issue Dec 25, 2024 · 7 comments
Closed
Assignees
Labels
Type: Bug Issue contains a bug related to a specific component. Something about the component is not working
Milestone

Comments

@root5427
Copy link

Describe the bug

Prehistory

Documentation states that:

The submit callback returns an object that encapsulates the form's validity, any existing errors, and its current state. This enables access to the form values, validation status, and any errors present at the time of submission.

Also it states that:

// e.values: An object containing the current values of all form fields.

export interface FormSubmitEvent {
/**
* The original DOM event.
*/
originalEvent: Event;
/**
* The form values.
*/
values: Record<string, any>;
/**
* The form state.
*/
states: Record<string, FormFieldState>;
/**
* Whether the form is valid.
*/
valid: boolean;
/**
* The form errors.
*/
errors: any[];
/**
* Resets the form.
*/
reset: () => void;
}

const handleSubmit = (callback) => {
return async (event) => {
const results = await validateOn('validateOnSubmit', true);
return callback({
originalEvent: event,
valid: toValue(valid),
states: toValue(states),
reset,
...results
});
};
};

Actual issue:

An object returned by submit callback DOES NOT have values field.

Reproducer

https://stackblitz.com/edit/primevue-nuxt-issue-template-qsshy1rm?file=app.vue

PrimeVue version

4.2.5

Vue version

3.x

Language

TypeScript

Build / Runtime

Nuxt

Browser(s)

Chrome 131

Steps to reproduce the behavior

  1. Install @primevue/forms.
  2. Use the Form component in a Vue application.
  3. Attempt to use the submit event as described in the documentation.
  4. Observe that object returned by submit callback does not contain values

Expected behavior

Existence of values in an object returned by submit callback.

@root5427 root5427 added the Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible label Dec 25, 2024
@tugcekucukoglu tugcekucukoglu added Status: Pending Review Issue or pull request is being reviewed by Core Team and removed Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible labels Dec 26, 2024
@tugcekucukoglu tugcekucukoglu added this to the 4.3.0 milestone Dec 26, 2024
@github-project-automation github-project-automation bot moved this to Review in PrimeVue Dec 26, 2024
@mertsincan mertsincan modified the milestones: 4.3.1, 4.3.0 Jan 6, 2025
@undefinedhuman
Copy link

I noticed it only contains the values object if you either set the initial values parameter or if you edit the input manually.

@undefinedhuman
Copy link

They somehow changed the entire behavior of the Forms library in one of the last minor versions.

@root5427
Copy link
Author

root5427 commented Jan 8, 2025

I noticed it only contains the values object if you either set the initial values parameter or if you edit the input manually.

Hi, @undefinedhuman.
I followed all the conditions you mentioned, but I still can't retrieve the values object. Could you provide a reproducible example, such as a link to a repository or a code snippet, where the values object exists in the submit callback return?

Thank you!

@undefinedhuman
Copy link

Sorry, my bad the states property gets populated not values:

<template>
  <Form v-slot="$form" class="flex flex-col w-full" @submit="onFormSubmit">
    <div class="flex flex-col gap-1">
      <InputText name="username" type="text" placeholder="Username" fluid />
      <Message v-if="$form.username?.invalid" severity="error" size="small" variant="simple">{{
        $form.username.error?.message
      }}</Message>
    </div>

    <Fieldset legend="Form States" class="h-80 overflow-auto">
      <pre class="whitespace-pre-wrap">{{ $form }}</pre>
    </Fieldset>
    <Button type="submit" severity="secondary" label="Submit" />
  </Form>
</template>

<script lang="ts" setup>
import { Form, type FormSubmitEvent } from "@primevue/forms";

function onFormSubmit(e: FormSubmitEvent) {
  console.log(e.states.username.value);
}
</script>

@mertsincan mertsincan added Type: Bug Issue contains a bug related to a specific component. Something about the component is not working and removed Status: Pending Review Issue or pull request is being reviewed by Core Team labels Jan 12, 2025
@github-project-automation github-project-automation bot moved this from Review to Done in PrimeVue Jan 12, 2025
@CamiloEspinoza
Copy link

First of all, I would like to thank everyone for their work; it is a wonderful library.

I would like to reopen this issue because, in the API section of the documentation, it still mentions that the FormSubmitEvent includes an object called values. This is very confusing.

@MomenYasser
Copy link

MomenYasser commented Feb 11, 2025

The PR not merged yet @mertsincan

@AVMiraga
Copy link

A temporary workaround I discovered is to pass the resolver to the Form. The resolver passed to the Form should always define all the fields, even if they are optional. If a field is omitted from the resolver, it will not be included in the submitted values (e.values).

For example, using a resolver like this:

const resolver = zodResolver(
  z.object({
    username: z.string().optional(),
    password: z.string().optional(),
  }),
);

ensures that both username and password are present in e.values. However, if the password field is not defined in the resolver, it doesn't appear in the values object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Issue contains a bug related to a specific component. Something about the component is not working
Projects
Status: Done
Development

No branches or pull requests

7 participants