question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

isRequired with Formik / React Hook Form

See original GitHub issue

Description

It would be nice to have the option to signify a field that it is required via Chakra UI API while using a 3rd party form validator tool

Problem Statement/Justification

Currently the isRequired attribute conflicts with libraries like Formik / React Hook Form

With Formik from Docs live edit: image

With React Hook Form browser’s validator kicks in image

Proposed Solution or API

It would be nice to have an attribute that would only add the asterisk and the aria attribute It can be called e.g. isRequiredIndicator, isRequiredIndicatorOnly, isRequiredAsterisk or isRequiredVisual, whatever

Alternatives

No response

Additional Information

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

7reactions
steven-prybylynskyicommented, Sep 29, 2021

Crafted a temporary solution

import { chakra, useStyles } from '@chakra-ui/react';
import { cx } from '@chakra-ui/utils';

/**
 * This component exists until there'll be a corresponding Chakra component available
 * @see https://github.com/chakra-ui/chakra-ui/issues/4770
 */
export function CbRequiredIndicator() {
  const styles = useStyles();
  const className = cx('chakra-form__required-indicator');

  return (
    <chakra.span
      children="*"
      className={className}
      __css={styles.requiredIndicator}
      role="presentation"
      aria-hidden="true"
    />
  );
}
2reactions
hixuscommented, Jul 28, 2022

In newer versions of chakra (at least 2.0.4) useStyles is deprecated, so it needs to be changed to useFormControlStyles

export function CbRequiredIndicator() {
  const styles = useFormControlStyles();
  const className = cx("chakra-form__required-indicator");

  return (
    <chakra.span
      className={className}
      __css={styles.requiredIndicator}
      role="presentation"
      aria-hidden="true"
    >
      *
    </chakra.span>
  );
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

React Hook Form vs. Formik: A technical ... - LogRocket Blog
Both Formik and React Hook Form are powerful libraries that will enable you to build any form you may need. They are architectured...
Read more >
FAQs | React Hook Form - Simple React forms validation
React Hook Form, Formik or Redux Form? First of all, all libs try to solve the same problem: make the form building experience...
Read more >
React Form Validation example with Hooks, Formik and Yup
Overview of React Hooks Form Validation example · Full Name: required · Username: required, from 6 to 20 characters · Email: required, email...
Read more >
React Forms - Formik vs. Hook-Form vs. Final-Form - Blog
React Hook Form is a form library built around React hooks. This library takes a different approach to building Form. It isolates component...
Read more >
Why You Should Choose React Hook Form Over Formik and ...
It's relatively easier to handle form validation in React Hook Form using the register hook. It also supports basic validation attributes such ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found