2.0.1 • Published 1 year ago

@hookform/error-message v2.0.1

Weekly downloads
34,579
License
MIT
Repository
github
Last release
1 year ago

npm downloads npm npm

Goal

A simple component to render associated input's error message.

Install

$ npm install @hookform/error-message

Quickstart

  • Single Error Message
import React from 'react';
import { useForm } from 'react-hook-form';
import { ErrorMessage } from '@hookform/error-message';

export default function App() {
  const { register, formState: { errors }, handleSubmit } = useForm();
  const onSubmit = (data) => console.log(data);

  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      <input
        name="singleErrorInput"
        ref={register({ required: 'This is required.' })}
      />
      <ErrorMessage errors={errors} name="singleErrorInput" />

      <ErrorMessage
        errors={errors}
        name="singleErrorInput"
        render={({ message }) => <p>{message}</p>}
      />

      <input name="name" ref={register({ required: true })} />
      <ErrorMessage errors={errors} name="name" message="This is required" />

      <input type="submit" />
    </form>
  );
}

  • Multiple Error Messages
import React from 'react';
import { useForm } from 'react-hook-form';
import { ErrorMessage } from '@hookform/error-message';

export default function App() {
  const { register, formState: { errors }, handleSubmit } = useForm({
    criteriaMode: 'all',
  });
  const onSubmit = (data) => console.log(data);

  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      <input
        name="multipleErrorInput"
        ref={register({
          required: 'This is required.',
          pattern: {
            value: /d+/,
            message: 'This input is number only.',
          },
          maxLength: {
            value: 10,
            message: 'This input exceed maxLength.',
          },
        })}
      />
      <ErrorMessage
        errors={errors}
        name="multipleErrorInput"
        render={({ messages }) =>
          messages &&
          Object.entries(messages).map(([type, message]) => (
            <p key={type}>{message}</p>
          ))
        }
      />

      <input type="submit" />
    </form>
  );
}

API

PropTypeRequiredDescription
namestringAssociated field name.
errorsobjecterrors object from React Hook Form. It's optional if you are using FormProvider.
messagestring \| React.ReactElementinline error message.
asstring \| React.ReactElement \| React.ComponentTypeWrapper component or HTML tag. eg: as="p", as={<p />} or as={CustomComponent}. This prop is incompatible with prop render and will take precedence over it.
renderFunctionThis is a render prop for rendering error message or messages. Note: you need to set criteriaMode to all for using messages.

Backers

Thank goes to all our backers! [Become a backer].

Contributors

Thanks goes to these wonderful people. [Become a contributor].

@quiltt/ui@uponco/admin-uibootflex2@swirepay/gateway-securereact-rds@frontastic/components@infinitebrahmanuniverse/nolb-_hoomy-test-dev@redocly/api-client@everything-registry/sub-chunk-400medusa-admin-ui-famino-uipj-login-packageoneto3ms-gateway-secure-testms-secure-test-onemr-commonsmylib-wing-stampix@cheryl2022/new-ui@choiceform/ui-reactc10amityuibootflexbraver-ui-reactbxx-mui-formcalendar-uicentricminds-uicgiframework@frontastic/theme-boost@front10/lab@applifyer/admin-ui@asurraa/sura-ui-hook-formatomic-elements@amityco/ui-kit@amityco/ui-kit-open-source@builtbypixel/nucleus@builtbypixel/nucleus-graphql@bitloop/ui-kit@bazyli/admin-uiadmin-ui-longvbye-design@air/plasmic@ajeebsa/admin-ui@angle-technologies/react-componentsviewrail-component-libclimb-ui-component@lrocher/medusa-admin-uimedusa-adminmedusajs-admin-uinapa-react-corereact-survey-builderreact-superformsreact-windy-uirds-react-sdkreact-api-formsreact-generated-formreact-crystalraketa-next-adminred-metrics-2-frontendrg-amity-social-uisndq-custom-componentsmweb-frontendtest-admin-uistyled-hook-formselect-field-searchable-xnselect-field-xntc-site-buildertc-site-builder-betareact-reportstorefront-icu_admin-uiui-ib-applicationuc-custom-medusajs-admin-uiuc-custon-medusajs-admin-uiupstra-uikitalgaeh-react-components@0xsplits/splits-kitdocusaurus-theme-openapi-docsdino_ui_tools@lieberweiss/react-offers@ljmarquezg/pj-claim-handle-portal@novapo/medusajs-admin-uigrab-a-plate-frontendgromet-hook-form@open-decision/design-system@open-legal-tech/design-system@medusajs/admin-ui@meetperry/amity-uikit@mezzanine-ui/react-hook-form@minternaljs/admin-ui@mxenabled/docusaurus-theme-openapi-docs@nelomobile/medusa-admin-ui@neonlaw/web@netlambda/admin-uicustom-no-code-form@imwebme/templateeiromplays-ui@rg-npmjs-dev/rg-amity-social-ui@dzangolab/react-form@eduzz/houston-formsinfinite-options-react@relationalai/rai-ux-lib
2.0.1

1 year ago

1.1.0

3 years ago

2.0.0

3 years ago

0.0.5-beta.1

3 years ago

0.0.5

3 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago