- Notifications
You must be signed in to change notification settings - Fork16
react-hook-form/error-message
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Performant, flexible and extensible forms with easy to use validation.
A simple component to render associated input's error message.
$ npm install @hookform/error-message
- Single Error Message
importReactfrom'react';import{useForm}from'react-hook-form';import{ErrorMessage}from'@hookform/error-message';exportdefaultfunctionApp(){const{ register,formState:{ errors}, handleSubmit}=useForm();constonSubmit=(data)=>console.log(data);return(<formonSubmit={handleSubmit(onSubmit)}><inputname="singleErrorInput"ref={register({required:'This is required.'})}/><ErrorMessageerrors={errors}name="singleErrorInput"/><ErrorMessageerrors={errors}name="singleErrorInput"render={({ message})=><p>{message}</p>}/><inputname="name"ref={register({required:true})}/><ErrorMessageerrors={errors}name="name"message="This is required"/><inputtype="submit"/></form>);}
- Multiple Error Messages
importReactfrom'react';import{useForm}from'react-hook-form';import{ErrorMessage}from'@hookform/error-message';exportdefaultfunctionApp(){const{ register,formState:{ errors}, handleSubmit}=useForm({criteriaMode:'all',});constonSubmit=(data)=>console.log(data);return(<formonSubmit={handleSubmit(onSubmit)}><inputname="multipleErrorInput"ref={register({required:'This is required.',pattern:{value:/d+/,message:'This input is number only.',},maxLength:{value:10,message:'This input exceed maxLength.',},})}/><ErrorMessageerrors={errors}name="multipleErrorInput"render={({ messages})=>messages&&Object.entries(messages).map(([type,message])=>(<pkey={type}>{message}</p>))}/><inputtype="submit"/></form>);}
Prop | Type | Required | Description |
---|---|---|---|
name | string | ✓ | Associated field name. |
errors | object | errors object from React Hook Form. It's optional if you are usingFormProvider . | |
message | string | React.ReactElement | inline error message. | |
as | string | React.ReactElement | React.ComponentType | Wrapper component or HTML tag. eg:as="p" ,as={<p />} oras={CustomComponent} . This prop is incompatible with proprender and will take precedence over it. | |
render | Function | This is arender prop for rendering error message or messages. Note: you need to set criteriaMode toall for using messages. |
Thank goes to all our backers! [Become a backer].
Thanks goes to these wonderful people. [Become a contributor].
About
📋 Error message component
Topics
Resources
License
Code of conduct
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
No packages published
Uh oh!
There was an error while loading.Please reload this page.
Contributors10
Uh oh!
There was an error while loading.Please reload this page.