Skip to content

Require entire matches by default with Constraint::matchesRegex() - #729

Merged
giuscris merged 2 commits into
2.xfrom
pattern-entire-matches
Jul 12, 2025
Merged

Require entire matches by default with Constraint::matchesRegex()#729
giuscris merged 2 commits into
2.xfrom
pattern-entire-matches

Conversation

@giuscris

Copy link
Copy Markdown
Member

This pull request introduces updates to regex patterns across multiple files to improve flexibility and consistency, as well as enhancements to the matchesRegex method to support partial or full regex matches.

Now patterns need to match the entire string by default, as if they were wrapped between ^(?: and )$.
This is to avoid unexpected matches and be consistent with the browser validation with pattern specified with the <input pattern="..."> attribute.

Below is a summary of the most important changes:

Method Enhancement:

  • Modified the matchesRegex method in formwork/src/Utils/Constraint.php to include an optional $entireMatch parameter, defaulting to true. This allows for both full and partial regex matches by dynamically appending anchors when needed.

@giuscris giuscris added this to the 2.0.0 milestone Jul 12, 2025
@giuscris
giuscris requested a review from Copilot July 12, 2025 11:14
@giuscris giuscris self-assigned this Jul 12, 2025
@giuscris giuscris added the enhancement New feature or request label Jul 12, 2025

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR standardizes regex usage by removing manual anchors from pattern definitions and enhances the matchesRegex utility to automatically enforce full-string matches by default (with an option for partial matches).

  • Removed ^/$ anchors from YAML and PHP pattern values
  • Added an $entireMatch parameter to matchesRegex (default true) to wrap patterns with ^(?:…)$
  • Updated the color field validator to use the new unanchored pattern

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
site/schemes/config/site.yaml Stripped ^/$ from language code pattern
panel/modals/newPage.yaml Removed anchors from slug pattern
formwork/schemes/config/system.yaml Updated file extension pattern to drop explicit ^/$ anchors
formwork/src/Utils/Constraint.php Added $entireMatch parameter and dynamic anchoring in matchesRegex
formwork/fields/color.php Switched to unanchored hex color pattern in validation
Comments suppressed due to low confidence (3)

formwork/src/Utils/Constraint.php:64

  • Update the docblock for matchesRegex to document the new $entireMatch parameter, including its default behavior and how it affects regex anchoring.
    /**

formwork/src/Utils/Constraint.php:69

  • Add unit tests to cover both default (full match) and partial match behaviors of matchesRegex, including cases with $entireMatch set to false.
        if ($entireMatch) {

site/schemes/config/site.yaml:57

  • [nitpick] Add a comment explaining that anchors (^,$) are no longer needed since matchesRegex enforces full-string matching by default.
        pattern: '[a-z]{2,3}'

@giuscris
giuscris merged commit a0133e1 into 2.x Jul 12, 2025
1 check passed
@giuscris
giuscris deleted the pattern-entire-matches branch July 12, 2025 11:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants