2.0.1 • Published 3 years ago

@hookform/error-message v2.0.1

Weekly downloads
34,579
License
MIT
Repository
github
Last release
3 years 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-fa@perseides/admin-uitc-site-buildertc-site-builder-betatac-formstorefront-icu_admin-uivendor-ui-alpha-v1@princemwase/medusa-admin-ui@payglo/admin-uijuicyfrontjsonxinput-field-address-xninput-field-masked-xninput-field-time-xninput-field-xninput-date-range-xnkey-esg-component-library@alisafavi123/admin-ui@tgthuan/mui-base-formreact-superformsstyled-hook-formsupersonic-dashboard-sdktxgw-docusaurus-theme-openapi-docsuc-custom-medusajs-admin-uiuc-custon-medusajs-admin-uiui-ib-applicationtest-admin-uitheme-openapi-writedocstp-react-elements-dev@air/plasmic@amityco/ui-kit@amityco/ui-kit-open-source@amityco/ulta-ui-kit@applifyer/admin-ui@angle-technologies/react-components@asurraa/sura-ui-hook-form@autoinvent/conveyor@autoinvent/conveyor-admin@ajeebsa/admin-ui@bazyli/admin-ui@bitloop/ui-kit@builtbypixel/nucleus@builtbypixel/nucleus-graphql@choiceform/ui-react@cheryl2022/new-ui@0xsplits/splits-kitye-designviewrail-component-libupstra-uikit@habbaldev78y/admin-ui@gusto/embedded-react-sdk@hadicph/admin-ui@formbricks/react@hitesh-lin/admin-ui@hernas/docusaurus-theme-openapi-docs@eduzz/ui-antd-hooks-form@eduzz/houston-forms@front10/lab@frontastic/theme-boost@lieberweiss/react-offers@ljmarquezg/pj-claim-handle-portal@lrocher/medusa-admin-ui@meclouds/admin-ui@medusajs/admin-ui@mezzanine-ui/react-hook-form@mezzanine-ui/react-hook-form-v2@mxenabled/docusaurus-theme-openapi-docs@neonlaw/web@netlambda/admin-ui@nelomobile/medusa-admin-ui@enzymefinance/hook-form@dzangolab/react-form@meetperry/amity-uikit@melnicenkovadik/my-component-library@minternaljs/admin-ui@medusajs/dashboard@imwebme/template@rafty/ui@rafty/fibr@radio-retail/ui@rezolabs/titledcms-nextjs@rg-npmjs-dev/rg-amity-social-ui@open-legal-tech/design-system@open-decision/design-system@port-labs/docusaurus-theme-openapi-docs@pointsyncc/ui@novapo/medusajs-admin-ui@phongsakron/ui-kit@optidist/admin-ui@sphinx-immer/op-kits
2.0.1

3 years ago

1.1.0

4 years ago

2.0.0

4 years ago

0.0.5-beta.1

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago