Skip to content

[Field] The initial value is not validated and the validation is inconsistent #1778

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

Closed
nsmag opened this issue Apr 25, 2025 · 2 comments · Fixed by #1779
Closed

[Field] The initial value is not validated and the validation is inconsistent #1778

nsmag opened this issue Apr 25, 2025 · 2 comments · Fixed by #1779
Labels
component: field type: bug Something doesn't work

Comments

@nsmag
Copy link

nsmag commented Apr 25, 2025

Bug report

Current behavior

Field does not validate the initial value and the validation is inconsistent and incorrect.

With this code:

<Field.Root>
  <Field.Label>Email</Field.Label>
  <Field.Control type="email" defaultValue="not_an_email@" />
</Field.Root>

<Field.Root>
  <Field.Label>Email</Field.Label>
  <Field.Control type="email" value="not_an_email@" />
</Field.Root>

The root div initially has only data-filled. When I focus then blur without typing anything, it gets data-valid. But if I type something (to make it dirty) and revert back to old value, it gets data-invalid. The input always has :invalid pseudo-class in these scenarios.

Expected behavior

  • Field should validate the initial value (I'm not sure if this behavior is intended or not).
  • The validation should be consistent between dirty value and non-dirty value.

Base UI version

v1.0.0-alpha.8

Which browser are you using?

Chrome

Which OS are you using?

Mac OS

@nsmag nsmag added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Apr 25, 2025
@nsmag nsmag changed the title [Field] the initial value is not validated and the validation is inconsistent [Field] The initial value is not validated and the validation is inconsistent Apr 25, 2025
@atomiks atomiks added type: bug Something doesn't work component: field and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Apr 25, 2025
@atomiks
Copy link
Contributor

atomiks commented Apr 25, 2025

Field should validate the initial value (I'm not sure if this behavior is intended or not).

This one is expected - it only validates on blur by default, but it requires it to have been marked dirty to reduce error noise, however this should probably only apply to required validation (to check empty values) rather than anything else

The validation should be consistent between dirty value and non-dirty value.

This one looks like a bug, addressing how required validation works should also fix this

@mj12albert
Copy link
Member

Field should validate the initial value

I was thinking if there was a realistic use-case for this and came across: https://github.com/orgs/react-hook-form/discussions/10267#discussioncomment-5784152

Another example could be when loading a shopping cart, the saved quantity/available quantity of something has changed so the initial value is invalid

Could the validate function be exposed with actionsRef so it can be called arbitrarily?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: field type: bug Something doesn't work
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants