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

[UForm] How can i access to submitter? #1984

Closed
ldiellyoungl opened this issue Jul 25, 2024 · 1 comment · Fixed by #2012
Closed

[UForm] How can i access to submitter? #1984

ldiellyoungl opened this issue Jul 25, 2024 · 1 comment · Fixed by #2012
Labels
question Further information is requested

Comments

@ldiellyoungl
Copy link

ldiellyoungl commented Jul 25, 2024

Description

Hello. How can i access to submitter in UFrom?

my code:

<script setup>
async function onSubmit(event) {
  switch (event.submitter.name) {
    case 'create':
      //some logic
      break;
    case 'update':
      //some logic
      break;
    case 'delete':
     //some logic
      break;
  }
}
</script>
<template>
  <UForm @submit="onSubmit">
    <UButton v-if="isCreateForm"  type="submit"name="create">
       create
    </UButton>
    <UButton  type="submit"name="update">
      update
    </UButton>
    <UButton  type="submit"name="delete">
      delete
    </UButton>
  </UForm/>
<template>

event i get:
image

@ldiellyoungl ldiellyoungl added the question Further information is requested label Jul 25, 2024
@ldiellyoungl ldiellyoungl changed the title how can i access to submitter? [UForm] how can i access to submitter? Jul 25, 2024
@ldiellyoungl ldiellyoungl changed the title [UForm] how can i access to submitter? [UForm]How can i access to submitter? Jul 25, 2024
@ldiellyoungl ldiellyoungl changed the title [UForm]How can i access to submitter? [UForm] How can i access to submitter? Jul 25, 2024
@DarlanPrado
Copy link

DarlanPrado commented Jul 31, 2024

Hey bro, I looked at your question, compare it with this code

https://github.com/nuxt/ui/blob/dev/docs/components/content/examples/FormExampleElements.vue

As you can see there is only one submit button on the form, the clear button calls a separate function

By closely observing the function of the component that makes the emit, we can see that the way it was done, it is impossible to know which of the buttons were clicked. so currently the component does not have support to do this

Submit Function in https://github.com/nuxt/ui/blob/dev/src/runtime/components/forms/Form.vue#L129

async function onSubmit (payload: Event) {
const event = payload as SubmitEvent
try {
if (props.validateOn?.includes('submit')) {
await validate()
}
const submitEvent: FormSubmitEvent = {
...event,
data: props.state
}
emit('submit', submitEvent)
} catch (error) {
if (!(error instanceof FormException)) {
throw error
}
const errorEvent: FormErrorEvent = {
...event,
errors: errors.value.map((err) => ({
...err,
id: inputs.value[err.path]
}))
}
emit('error', errorEvent)
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
2 participants