Problem/Motivation

Using invalid regex strings result in errors like the following:

Warning: preg_match(): Unknown modifier ',' in Symfony\Component\Validator\Constraints\RegexValidator->validate() (line 47 of /var/www/html/vendor/symfony/validator/Constraints/RegexValidator.php).

Steps to reproduce

Set up a field validation with the regex plugin using a string like this: (.*),(.*)
Fill in a value into the entity form like this: something, blah
Note that the error doesn't happen if the regex string has a suffix

Proposed resolution

Work out whether the pattern fields should be validated, or whether help text could be provided to guide site builders to entering strings in the correct format.

Remaining tasks

TBD

User interface changes

TBD

API changes

TBD

Data model changes

TBD

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

DamienMcKenna created an issue. See original summary.

damienmckenna’s picture

Issue summary: View changes
damienmckenna’s picture

The problem was that the regex strings needed prefix and suffix slashes, but the UI didn't make that clear, once I added the slashes it worked as expected.

damienmckenna’s picture

Title: Valid regex hit "Unknown modifier" errors » Protect / help avoid validation logic problems
Component: Code » User interface
Category: Bug report » Feature request
Issue summary: View changes

Turning this around to focus on improving the UI, it was user error that could be guarded against rather than a bug in the module.

pcambra’s picture

100% agreed with this, regex validators should be... validated, both indicating the format expected and preventing saving the wrong format. Or just removing the need to append the slashes.

marc.bau’s picture

I guess I had the same issue here...

Migrated several regex rules from D7 to D10 and wondered why they all erroring with preg_match(): No ending delimiter '^. But https://stackoverflow.com/questions/4634993/php-regular-expressions-no-e... provided the solution. The D10 module requires leading and trailing slashes!

Example - Telephone number validation:

D7:
^(?:(\+\d{1,2})\s(?:\(\d{1,}\))\s(?:\d+[-.\s]?+)\d{1,})$

D10
/^(?:(\+\d{1,2})\s(?:\(\d{1,}\))\s(?:\d+[-.\s]?+)\d{1,})$/

Is this an intentional change or a defect in the module?

abhisekmazumdar’s picture

If someone can make a MR for this bug it will really helpful...
I'm trying to make a 3.0.0 stable version soon.

abhisekmazumdar’s picture

abhisekmazumdar’s picture

Status: Active » Needs review

I opened an MR for this: https://git.drupalcode.org/project/field_validation/-/merge_requests/25. Marking this needs review.

Invalid regex patterns (missing delimiters, malformed syntax) no longer leak a raw PHP warning. They're now caught and shown as a clean validation error instead.

How to test

  1. Create a field validation rule using the Regex plugin.
  2. Enter a pattern missing delimiters, e.g. (.*),(.*).
  3. Try to save the rule, or run it against a field value.
  4. You should see a clean validation error instead of a PHP warning.
ahmad-khalil-imagex’s picture

Assigned: Unassigned » ahmad-khalil-imagex
arianraeesi’s picture

Issue tags: +AI Initiative Sprint, +ai inside, +2027Sprint17
ahmad-khalil-imagex’s picture

Assigned: ahmad-khalil-imagex » Unassigned
arianraeesi’s picture

Issue tags: -2027Sprint17 +2026Sprint17
arianraeesi’s picture

Issue tags: +2026Sprint18

  • abhisekmazumdar committed cc9ba2e7 on 3.0.x
    Issue #3390907: Trap invalid regex patterns instead of leaking raw PHP...
abhisekmazumdar’s picture

Status: Needs review » Reviewed & tested by the community

MR !25 is merged. Tested end to end, looking good.

Thanks everyone for the work
DamienMcKenna for the original report,
ahmad-khalil-imagex for the thorough review, and everyone else who chimed in.

abhisekmazumdar’s picture

Status: Reviewed & tested by the community » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.