Skip to content

[11.x] Fluent numeric validation#54425

Merged
taylorotwell merged 3 commits into
laravel:11.xfrom
xoesae:feat/fluent-numeric-validation
Feb 5, 2025
Merged

[11.x] Fluent numeric validation#54425
taylorotwell merged 3 commits into
laravel:11.xfrom
xoesae:feat/fluent-numeric-validation

Conversation

@xoesae

@xoesae xoesae commented Jan 31, 2025

Copy link
Copy Markdown
Contributor

Description

This pull request introduces a new fluent numeric validation class, offering a more elegant and expressive way to define numeric validation rules.

Before this PR, numeric validation required string-based rules:

$rules = [
    'score' => 'numeric|integer|multiple_of:10|lte:some_field|max:100',
];

$validator = Validator::make($data, $rules);

if ($validator->fails()) {
    return $validator->errors();
}

To use the fluent numeric validation:

use Illuminate\Validation\Rule;

$rules = [
    'score' => [
        Rule::numeric()
            ->integer()
            ->multipleOf(10)
            ->lessThanOrEqual('some_field')
            ->max(100);
    ],
];

$validator = Validator::make($data, $rules);

if ($validator->fails()) {
    return $validator->errors();
}

@taylorotwell

Copy link
Copy Markdown
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!

@taylorotwell taylorotwell reopened this Feb 5, 2025
@taylorotwell taylorotwell merged commit 75b6392 into laravel:11.x Feb 5, 2025
crynobone added a commit that referenced this pull request Feb 5, 2025
fixes tests failure introduced in #54425

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
@crynobone crynobone mentioned this pull request Feb 5, 2025
taylorotwell pushed a commit that referenced this pull request Feb 5, 2025
* Test Improvements

fixes tests failure introduced in #54425

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

---------

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
@siarheipashkevich

Copy link
Copy Markdown
Contributor

@xoesae what about the strict type?
https://laravel.com/docs/12.x/validation#rule-numeric

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