Skip to content

Commit

Permalink
Feat: getValidators() function for react-hook-form
Browse files Browse the repository at this point in the history
  • Loading branch information
justiandevs committed Oct 24, 2024
1 parent 4442f03 commit 5682921
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react'
import { useTranslations } from 'next-intl'
import { useFormStore } from '@/store/form_store'
import { QuestionField } from '@/types/form'
import { getValidators } from '@/lib/utils/form-validator'

interface RadioGroupProps extends QuestionField {}

Expand Down Expand Up @@ -47,9 +48,7 @@ export const RadioInput = ({ field, register, errors }: RadioGroupProps) => {
{Object.keys(field.meta.values).map((key: string) => (
<div key={key}>
<input
{...register(field.key, {
required: field.required ? t('required') : false,
})}
{...register(field.key, getValidators(field, t))}
type="radio"
id={`${field.key}-${key}`}
value={key}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { QuestionField } from '@/types/form'
import React from 'react'
import { useTranslations } from 'next-intl'
import { useFormStore } from '@/store/form_store'
import { getValidators } from '@/lib/utils/form-validator'

interface TextInputProps extends QuestionField {}

Expand Down Expand Up @@ -45,9 +46,7 @@ export const TextInput = ({ field, register, errors }: TextInputProps) => {
<span id={`${field.key}-${field.key}`}>{field.meta.subtitle}</span>
)}
<input
{...register(field.key, {
required: field.required ? t('required') : false,
})}
{...register(field.key, getValidators(field, t))}
type="text"
placeholder={field.meta.placeholder ? field.meta.placeholder : ''}
defaultValue={getDefaultValueTextInput(field.key)}
Expand Down
40 changes: 40 additions & 0 deletions src/lib/utils/form-validator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { PublicQuestion } from '@/types/form'
import { RegisterOptions } from 'react-hook-form'

export const getValidators = (
field: PublicQuestion,
t: any
): RegisterOptions => {
const validators: RegisterOptions = {}

// If field is required, set required validation rule
if (field.required) {
validators.required = t('required')
}

// If validators exist on the meta, loop through this
if (field.meta?.validators) {
field.meta.validators.forEach(
(validatorRule: string | [string, number]) => {
if (typeof validatorRule === 'string') {
// Handle single validator 'required'
if (validatorRule === 'required') {
validators.required = field.required ? t('required') : false
}
} else if (Array.isArray(validatorRule)) {
// Handle validators with parameters like ['maxLength', 100]
const [validatorName, validatorValue] = validatorRule

if (validatorName === 'maxLength') {
validators.maxLength = {
value: validatorValue,
message: t('max_length', { maxLength: validatorValue }),
}
}
}
}
)
}

return validators
}
3 changes: 2 additions & 1 deletion translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@
"submit_button": "Submit"
},
"errors": {
"required": "This field is required"
"required": "This field is required",
"max_length": "This field has a maximal length of {maxLength} characters"
}
},
"routing": {
Expand Down
3 changes: 2 additions & 1 deletion translations/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@
"submit_button": "Verstuur"
},
"errors": {
"required": "Dit veld is verplicht"
"required": "Dit veld is verplicht",
"max_length": "Dit veld heeft een maximale lengte van {maxLength} karakters"
}
},
"routing": {
Expand Down

0 comments on commit 5682921

Please sign in to comment.