123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
- import type { FormKitNode, FormKitProps } from '@formkit/core'
- const addRequired = (
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- validation: string | Array<[rule: string, ...args: any]>,
- ) => {
- if (Array.isArray(validation)) {
- if (!validation.includes(['required'])) validation.push(['required'])
- return validation
- }
- if (!validation) {
- return 'required'
- }
- if (!validation.includes('required')) {
- return `${validation}|required`
- }
- return validation
- }
- const removeRequired = (
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- validation: string | Array<[rule: string, ...args: any]>,
- ) => {
- if (!validation) return validation
- if (Array.isArray(validation)) {
- return validation.filter(([rule]) => rule !== 'required')
- }
- if (validation.includes('required')) {
- return validation
- .split('|')
- .filter((rule: string) => !rule.includes('required'))
- .join('|')
- }
- return validation
- }
- const addRequiredToValidationProp = (props: Partial<FormKitProps>) => {
- const { validation } = props
- props.validation = addRequired(validation)
- }
- const removeRequiredFromValidationProp = (props: Partial<FormKitProps>) => {
- const { validation } = props
- props.validation = removeRequired(validation)
- }
- const addRequiredValidation = (node: FormKitNode) => {
- const { props, context } = node
- if (!props.definition || !context || node.type !== 'input') return
- node.addProps(['required'])
- if (props.required) {
- addRequiredToValidationProp(props)
- }
- node.hook.prop(({ prop, value }, next) => {
- if (prop === 'validation') {
- if (props.required) {
- value = addRequired(value)
- } else {
- value = removeRequired(value)
- }
- }
- return next({ prop, value })
- })
- node.on('prop:required', ({ payload }) => {
- if (payload) {
- addRequiredToValidationProp(props)
- } else {
- removeRequiredFromValidationProp(props)
- }
- })
- }
- export default addRequiredValidation
|