I'm using useController for my text inputs, but I'm not able to set an error message for a custom validation. I assumed I would be able to use an object with value/message similar to the built in validations, but I get the error TypeError: validateFunction is not a function if I try to do this.
This works fine:
<TextInput
control={control}
name={`email`}
label={`Email *`}
rules={{
required:true,
validate: {
checkEmail: v => validateEmail(v)
}
}}
/>
What I want to do:
<TextInput
control={control}
name={`email`}
label={`Email *`}
rules={{
required:true,
validate: {
checkEmail: {
value: v => validateEmail(v),
message: 'Invalid email'
}
}
}}
/>
My TextInput component currently looks like this:
import style from "./form.module.scss"
import classnames from "classnames";
import { useController, useForm } from "react-hook-form";
export default function TextInput(props){
const {
field: { ref, ...inputProps },
fieldState: { invalid, isTouched, isDirty },
formState: { touchedFields, dirtyFields, errors }
} = useController({
name: props.name,
control: props.control,
rules: props.rules,
defaultValue: props.defaultValue ? props.defaultValue : '',
});
return(
<div className={classnames(style.fieldItem, {[style.error]: invalid})}>
<label className={style.label}>{props.label}</label>
<input
className={style.textInput}
inputRef={ref}
type={`text`}
placeholder={props.placeholder}
{...inputProps}
/>
{invalid &&
<div className={style.message}>{`Invalid input`}</div>
}
</div>
);
}
How can I set an error message for custom validations when using this approach?