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

[10.x] Allow digits rules to work with decimal #47976

Closed
wants to merge 2 commits into from

Conversation

timacdonald
Copy link
Member

@timacdonald timacdonald commented Aug 7, 2023

It is currently possible to limit the number of digits in an integer:

'int_number' => ['required', 'integer', 'max_digits:5'],

It is also currently possible to limit the number of digits after the decimal place in an decimal:

'dec_number' => ['required', 'decimal:0,5'],

However, it is not possible to limit the total number of digits of a decimal. It would be useful to be able to specify the total number of digits.

Validator::validate([
    'number' => 123.456,
], [
    'number' => ['decimal:0,3', 'max_digits:6'], // passes

    'number' => ['decimal:0,3', 'max_digits:5'], // fails. 3 before dot and 3 after = 6 digits
])

Documented: laravel/docs#8943

@henzeb
Copy link
Contributor

henzeb commented Aug 7, 2023

And what is wrong with decimal:3,3 and decimal:2,3? Are you more looking for allowing it cases like 12,345 or 123,45 but never 123,456?

@buddhaCode
Copy link

Hey @timacdonald, I was stumbling across the same issue, lately. Why did you close your PR?

@timacdonald
Copy link
Member Author

@buddhaCode, I can't remember exactly, but I believe there is already a way to do this with Laravel rules.

@buddhaCode
Copy link

Mhhh, I don't see any way to do this with existing rules in Laravel. Can you tell me, what way you have in mind?

Dan linked my PR for Filament above my comment. Maybe you can have a look at it. And maybe you can resubmit your PR?

Nether the less, I think a rule to validate the length before the dot would be a lot more practical in real use cases. I couldn't imagine a real use case, where the total length is important.
But when I have a $table->decimal(8,3) column in database, I would like to validate the part before the dot, to make sure it fits into the database with something like decimal_whole,8|decimal,3. Don't know what a good name for the part before the dot would be, because I'm not an english native speaker.

@timacdonald
Copy link
Member Author

timacdonald commented Mar 15, 2024

@buddhaCode, I think I found that a combination of max and decimal did the trick. You may also need a min rule.

This example uses a decimal(5, 3) column.

class ExampleTest extends TestCase
{
    /**
     * @dataProvider data
     */
    public function test_validation($expected, $value): void
    {
        $validator = Validator::make([
            'value' => $value,
        ], [
            'value' => 'max:99.999|decimal:0,3' // $table->decimal(5, 3)
        ]);

        $this->assertSame($expected, $validator->passes());
    }

    public static function data()
    {
        return [
            // lower boundary...
            [true,  0.999 ],
            [false, 0.9991],
            // upper boundary...
            [true, 99.999],
            [false, 100.0],
        ];
    }
}

@buddhaCode
Copy link

buddhaCode commented Mar 18, 2024

Oh thats a good solution. But I guess less obvious and less good readable than your PR. Also you need to have knowledge about the desired amount of decimal places.
I would be happy to see your PR in Laravel.

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