Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react-hook-form Set error message for custom validation when using useController

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?

like image 531
Chris Avatar asked Apr 18 '26 16:04

Chris


1 Answers

For a custom validation using validate you have to pass the error message directly:

<TextInput
  control={control}
  name={`email`}
  label={`Email *`}
  rules={{
    required:true,
    validate: {
      checkEmail: v => validateEmail(v) || "ERROR MESSAGE"
    }
  }}
/>

You can read about it in this section about register as register and <Controller /> or useController are using the same rules interface.

like image 174
knoefel Avatar answered Apr 20 '26 05:04

knoefel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!