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
Issue fork field_validation-3390907
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
Comment #2
damienmckennaComment #3
damienmckennaThe 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.
Comment #4
damienmckennaTurning this around to focus on improving the UI, it was user error that could be guarded against rather than a bug in the module.
Comment #5
pcambra100% 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.
Comment #6
marc.bauI 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?
Comment #7
abhisekmazumdarIf someone can make a MR for this bug it will really helpful...
I'm trying to make a 3.0.0 stable version soon.
Comment #8
abhisekmazumdarComment #10
abhisekmazumdarI 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
(.*),(.*).Comment #11
ahmad-khalil-imagexComment #12
arianraeesi commentedComment #13
ahmad-khalil-imagexComment #14
arianraeesi commentedComment #15
arianraeesi commentedComment #17
abhisekmazumdarMR !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.
Comment #18
abhisekmazumdar