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

Cannot read $form form field states with TypeScript #6723

Closed
spiffytech opened this issue Nov 4, 2024 · 10 comments
Closed

Cannot read $form form field states with TypeScript #6723

spiffytech opened this issue Nov 4, 2024 · 10 comments
Assignees
Labels
Type: Bug Issue contains a bug related to a specific component. Something about the component is not working
Milestone

Comments

@spiffytech
Copy link

spiffytech commented Nov 4, 2024

Describe the bug

I'm using the new forms API, and I get TypeScript errors when I try to access form field states on the $form object.

This code reproduces it. The template is just a copy/paste of some sample code in the Forms docs.

Accessing $form.username produces this error:

Property 'username' does not exist on type '{ register: (field: string, options: any) => any; reset: () => void; valid: boolean; states: Record<string, FormFieldState>; }'

<script setup lang="ts">
import { Form } from '@primevue/forms'
import Button from 'primevue/button'
</script>

<template>
  <Form v-slot="$form" class="flex flex-col gap-4 w-full sm:w-56">
    <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>
    <Button type="submit" severity="secondary" label="Submit" />
  </Form>
</template>

Reproducer

https://stackblitz.com/edit/primevue-4-ts-vite-issue-template-kef9dg?file=src%2FApp.vue

PrimeVue version

4.2.1

Vue version

3.x

Language

TypeScript

Build / Runtime

Vite

Browser(s)

No response

Steps to reproduce the behavior

No response

Expected behavior

No response

@spiffytech spiffytech 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 4, 2024
@jackytank
Copy link

I have the same issue

@NutNut17
Copy link

NutNut17 commented Nov 6, 2024

I have a similar issues. I just getting started using Forms. After I followed all the steps shown in the documentation, the Fieldset component showed that the initialValues was not bound to $form even that I add it as Form property.

@mertsincan mertsincan 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 Nov 6, 2024
@mertsincan mertsincan added this to the 4.2.2 milestone Nov 6, 2024
@fgrzesiak
Copy link

I have the same issue as well.

@SimonOyaneder
Copy link

Same issue here

@carloeusebi
Copy link

Same

@Roeselmann
Copy link

same!

@tugcekucukoglu tugcekucukoglu modified the milestones: 4.2.2, 4.2.3 Nov 14, 2024
@tugcekucukoglu tugcekucukoglu modified the milestones: 4.2.3, 4.2.4 Nov 22, 2024
@tugcekucukoglu tugcekucukoglu modified the milestones: 4.2.4, 4.3.0 Nov 26, 2024
@Kitketovsky
Copy link

I have the same issue, short term solution is to use their FormField component

@AlexMountainBN
Copy link

AlexMountainBN commented Dec 9, 2024

My workaround looks like this:

<Form
    v-slot="$form"
    :resolver="resolver"
    :initialValues="initialValues"
    @submit="onFormSubmit"
    class="flex flex-col gap-4 w-full sm:w-56">
    <div class="flex flex-col gap-1">
      <InputNumber name="amount" fluid />
      <Message
        v-if="
          // @ts-expect-error an issue in primevue leads to compile errors
          $form.amount?.invalid
        "
        severity="error"
        size="small"
        variant="simple"
        >{{
          // @ts-expect-error an issue in primevue leads to compile errors
          $form.amount.error?.message
        }}</Message
      >
    </div>
    <Button type="submit" severity="secondary" label="Submit" />
  </Form>

I think it's better to revert if the issue is solved.

@Quang-Dong
Copy link

Same.

@Quang-Dong
Copy link

Quang-Dong commented Dec 11, 2024

I do this now so TS doesn't catch errors..

<Message
     v-if="($form as any).account?.invalid"
     severity="error"
     size="small"
     variant="simple">
     {{ ($form as any).account.error?.message }}
</Message>

@mertsincan mertsincan modified the milestones: 4.3.1, 4.3.0 Jan 6, 2025
@mertsincan mertsincan moved this to Review in PrimeVue Jan 6, 2025
@mertsincan mertsincan self-assigned this Jan 6, 2025
@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 6, 2025
@github-project-automation github-project-automation bot moved this from Review to Done in PrimeVue Jan 6, 2025
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