utils.ts 957 B

12345678910111213141516171819202122232425
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { getNode, type FormKitNode } from '@formkit/core'
  3. import UserError from '#shared/errors/UserError.ts'
  4. import type { MutationSendError } from '../../types/error.ts'
  5. export const getNodeId = (formId: string, selector: string) => {
  6. return `${selector}-${formId}`
  7. }
  8. export const getNodeByName = (formId: string, selector: string) => {
  9. return getNode(getNodeId(formId, selector))
  10. }
  11. export const setErrors = (node: FormKitNode, errors: MutationSendError) => {
  12. // TODO: we need to check if translations are working as expected for this errors here.
  13. // TODO: we need to check/style the general error output when we want to show it related to the form.
  14. if (errors instanceof UserError) {
  15. node.setErrors(errors.generalErrors as string[], errors.getFieldErrorList())
  16. } else {
  17. node.setErrors(errors?.message || __('An unexpected error has occurred.'))
  18. }
  19. }