Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If a form field is ever switched to required, it will always be required #299

Open
2 tasks done
EkaLinMan opened this issue Aug 19, 2024 · 5 comments
Open
2 tasks done

Comments

@EkaLinMan
Copy link

EkaLinMan commented Aug 19, 2024

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Current behavior 😯

Please check the demo, if the TextFieldElement or SelectElement is ever switched to required, it will always be required even if the required state is switched back to false.

Expected behavior 🤔

The required state of the form field should correspond to the real state passed from the required attribute.

@EkaLinMan
Copy link
Author

EkaLinMan commented Aug 19, 2024

We found a related issue in RHF, hoping it can help, thanks!
react-hook-form/react-hook-form#1749

@EkaLinMan EkaLinMan changed the title If a TextFieldElement is ever switched to required, it will always be required If a form field is ever switched to required, it will always be required Aug 22, 2024
@EkaLinMan
Copy link
Author

EkaLinMan commented Sep 3, 2024

We found the root cause, RHFM passes the empty object as the rule and leads to the issue, please check the demo, thanks! https://codesandbox.io/p/sandbox/broken-cookies-38y28p

image

@sadik-malik
Copy link
Contributor

@EkaLinMan, use unregister when changing the isRequired property or anything in rules prop. React Hook Form will then re-register the component with the updated rules in next render.

@EkaLinMan
Copy link
Author

EkaLinMan commented Sep 4, 2024

@sadik-malik, thanks for your reply, we overrode the required rule to fix this issue, this way also works.
{required: required ? "This field is required" : false}

But I'm afraid that the TextFieldElement is not the only one that has this issue. It would be great if RHFM could push a new version to fix all these issues in the future.

@dohomi
Copy link
Owner

dohomi commented Oct 10, 2024

@EkaLinMan could you elaborate why providing your own rules is not the solution for this problem? I use the rules object in all my project because I need translated versions of the required error message anyway, that might be the reason why this issue was never present in my projects. To rely on required prop only is just a "lazy" shortcut. I am happy to review a PR though to fix it in your favour.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants