Validating array of email
?
#1338
MichaelSchmidle
started this conversation in
General
Replies: 1 comment
-
For your convenience, this is the solution I came up with by reusing FormKit's existing // formkit/emailArray.ts
import type { FormKitNode } from '@formkit/core'
import { email as emailValidation } from '@formkit/rules'
const emailArray = function (node: FormKitNode<string[]>): boolean {
if (!Array.isArray(node.value)) {
return false
}
// Check each email in the array with FormKit's built-in validation
return node.value.every(email => {
// Creating a temporary node object to use with the emailValidation function
const tempNode: FormKitNode = {
...node, // Spreading the original node to inherit properties
value: email // Setting the value to the current email string
}
return emailValidation(tempNode)
})
}
// Optional: Override default rule behaviors if needed
emailArray.blocking = true
emailArray.skipEmpty = true
export default emailArray // formkit.config.ts
// Add:
import emailArray from './formkit/emailArray'
return {
// Existing code.
// Add:
rules: {
emailArray,
},
// Existing code.
} In your FormKit components, the rule now becomes available with Maybe some of the FormKit devs can confirm that's the way to go? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm using the Taglist to collect email addresses. Using the validation rule
email
is of course wrong since I don't want to validate a string as email but an array of strings as emails. The docs about validation or the Taglist don't mention how to validate elements of an array. So, I wonder:What's the best way to validate an array of emails? (I.e.,
['[email protected]', '[email protected]']
.) Thanks in advance for your support.Beta Was this translation helpful? Give feedback.
All reactions