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

Field Validation doesn't seem to respect detecting a null or an empty string as part of a regexp. #21489

Open
MarkBurvs opened this issue Feb 20, 2024 · 1 comment

Comments

@MarkBurvs
Copy link

MarkBurvs commented Feb 20, 2024

Describe the Bug

I’m trying to add basic validation to an input field where I want the user to enter either a valid email address, or nothing at all.

I’m using regex and in theory this should work: (^$|^.@...*$), (it works as expected on https://regex101.com), only it doesn’t allow the user to delete an email that was previously entered:

Screenshot 2024-02-20 at 14 57 07

After deleting a previously saved value:
Screenshot 2024-02-20 at 15 41 08

The only way I can get the desired behaviour is by creating an “Or” group, but that gives a double error message when the user enters anything wrong which is ugly and confusing:
Screenshot 2024-02-20 at 15 22 18
Screenshot 2024-02-20 at 12 45 36

Had a short discussion on the Discord about this:
https://discord.com/channels/725371605378924594/1151320093565927504/1209480440944005130

To Reproduce

Create a text input field called "email" within a collection.
Ensure the field allows a NULL value.
Add the following regexp validation rule to detect a correctly formatted email OR an empty string: (^$|^.@...*$)
Go to the Content section, create an item and enter a valid email into the "email" field.
Save.
Value should be saved correctly.
Clear the email field again so it is empty.
Save again.
Validation will fail. Expected result is that it should accept an empty field

Note:
You can test any of the following Regexp alternatives on https://regex101.com and observe that they do correctly match against an empty string as well as an email address, but try them in Directus, and they will fail when the field is empty:
(^$|^.*@.*\..*$)
^(.*@.*)?$
(^NULL$|^.*@.*\..*$)

Directus Version

v10.9.2

Hosting Strategy

Self-Hosted (Docker Image)

@hanneskuettner
Copy link
Contributor

I would indeed not expect a RegExp which is intended to match a string after all to match the value null.
You can circumvent this be setting "Cleared Value" to "Save as empty string" in the interface configuration, ensuring that the value is at least always an empty string. With your regex and this will however encounter an error, that is tracked in #22394.

I will leave this issue open as it's correct solution relies on #22394 to be fixed.

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

No branches or pull requests

2 participants