form
form copied to clipboard
[Feature] Run validation on specific field
Hey, it would be great to have a functionality to run validation on a specific field. for example, i have input with amount that the minimum amount is depended on a currency selection input, every time i change the currency the minimum amount is changed thus the minimum amount validation check should fire.
Do you think it's possible to make it happen ?
Thank you
In the meanwhile, i've implemented this with useImperativeHandle
import React, { useImperativeHandle } from 'react'
import TextField from '../textfield'
import { useField, splitFormProps } from 'react-form'
export const TextFieldValidation = React.forwardRef((props, ref) => {
const [field, fieldOptions, rest] = splitFormProps(props)
const {
value = '',
meta: { error, isTouched },
getInputProps,
runValidation,
} = useField(field, fieldOptions)
useImperativeHandle(ref, () => ({
runValidation: () => runValidation(),
}))
return (
<TextField
value={value}
{...getInputProps({ ref, ...rest })}
error={isTouched && error ? error : undefined}
/>
)
})
export default TextFieldValidation