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

[11.x] Add fluent RegExp validation class rule #54521

Closed
wants to merge 17 commits into from

Conversation

shaedrich
Copy link
Contributor

@shaedrich shaedrich commented Feb 8, 2025

Similar to #54488, #53465, #54425, #54517, and #54519

Examples

Simplest case

use Illuminate\Validation\Rule;

public function rules()
{
    return [
        'foo' => [
            'required',
            Rule::regex('/[a-z]/'), // results in 'regex:/[a-z]/'
        ],
    ];
}

Negation

use Illuminate\Validation\Rule;

public function rules()
{
    return [
        'foo' => [
            'required',
            Rule::regex('/[a-z]/')->not(), // results in 'not_regex:/[a-z]/'
        ],
    ];
}

Flags

use Illuminate\Validation\Rule;

public function rules()
{
    return [
        'foo' => [
            'required',
            Rule::regex('/[a-z]/i'), // results in 'regex:/[a-z]/i'
            // or Rule::regex('/[a-z]/', ['i'])
            // or Rule::regex('/[a-z]/')->flags(['i'])
        ],
    ];
}

Modify flags

use Illuminate\Validation\Rule;

public function rules()
{
    $rule = Rule::regex('/[a-z]/i');
    $rule->flag('g');
    return [
        'foo' => [
            'required',
            $rule, // results in 'regex:/[a-z]/gi'
        ],
    ];
}

@shaedrich shaedrich marked this pull request as draft February 8, 2025 16:05
@shaedrich shaedrich marked this pull request as ready for review February 8, 2025 16:12
@AndrewMast
Copy link

I like this suggestion! I have a few comments that I feel like will help it be inline with the usual Laravel code that I will add in a review/comments.

@shaedrich
Copy link
Contributor Author

@AndrewMast Sure, feedback is always welcome 👍🏻

src/Illuminate/Validation/Rules/RegExp.php Outdated Show resolved Hide resolved
src/Illuminate/Validation/Rules/RegExp.php Outdated Show resolved Hide resolved
src/Illuminate/Validation/Rules/RegExp.php Outdated Show resolved Hide resolved
':',
$this->regExp,
)
->when($this->flags, function (SupportStringable $str) {

Choose a reason for hiding this comment

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

What if we get all of the flags from the expression when we create the instance? This way if we call ->flags('i', 'g', ...) (which current replaces all flags with the flags provided), it will not keep any of the flags originally in the expression. Then we can just add implode('', $this->flags) to the ->append(...) here.

Choose a reason for hiding this comment

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

Maybe we could do something like this for the constructor:

public function __construct(string $expression, array|Arrayable $extraFlags = [])
{
    $currentFlags = str_split(Str::of($expression)->afterLast('/'));

    if ($extraFlags instanceof Arrayable) {
        $extraFlags = $extraFlags->toArray();
    }
    
    $this->expression = Str::of($expression)->beforeLast('/')->append('/')->toString();
    $this->flags = array_unique([...$currentFlags, ...$extraFlags]);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

which current replaces all flags with the flags provided

That was the idea. The class has essentially two methods for this:

  • setFlags(): Entirely replaces the flags (named flags() due to naming conventions)
  • addFlag(): Adds a flag to the list of flags without replacing them (named flag() due to naming conventions

Choose a reason for hiding this comment

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

That's what I assumed! Now with the updated constructor code, it makes much more sense to me. Good work!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good work!

Primarily due to your feedback 👍🏻 Thanks a lot!

@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions!

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

Successfully merging this pull request may close these issues.

3 participants