Skip to content

[8.x] Fix multiple dots for digits_between rule#42330

Merged
taylorotwell merged 6 commits into8.xfrom
fix-multiple-dots-in-rule
May 10, 2022
Merged

[8.x] Fix multiple dots for digits_between rule#42330
taylorotwell merged 6 commits into8.xfrom
fix-multiple-dots-in-rule

Conversation

@driesvints
Copy link
Copy Markdown
Member

@driesvints driesvints commented May 10, 2022

This makes the digits_between rule a bit more strict by validating for multiple dots in a number (2.2.3, 2..3, ...).

Fixes #42326

@driesvints driesvints changed the title Fix multiple dots for digits_between rule [8.x] Fix multiple dots for digits_between rule May 10, 2022
@taai
Copy link
Copy Markdown
Contributor

taai commented May 10, 2022

Possibilities to consider (for example, digits_between:1,10):

  • single dot
  • dot at the end (some may argue that it is valid, not me)
  • dot at the beginning (some may argue that it is valid, not me)

@driesvints
Copy link
Copy Markdown
Member Author

@taai ah the single dot is a good remark. Added a fix and test for that. The other two examples are indeed valid imo: https://3v4l.org/iDrQh

@taai
Copy link
Copy Markdown
Contributor

taai commented May 10, 2022

  • I suggest using substr_count($value, '.') to count the dots
  • Why to convert/cast the value to string so many times?
  • What if the value is not a string and is not a number, but is an array?
  • I suggest doing simple, lightweight stuff (comparison operations) before doing hard stuff (like regex)
  • I suggest using ctype_digit() which is significantly faster than regex

⚠️ N.B. Should also allow dots in validation rule digits – for consistency...

My solution:

public function validateDigitsBetween($attribute, $value, $parameters)
{
    $this->requireParameterCount(2, $parameters, 'digits_between');

    // make sure that the value is a number or a numeric string
    if (!is_numeric($value)) {
        return false;
    }

    if (is_string($value)) {
        // make sure that the value is not a dot and does not contain more than single dot
        if ($value === '.' || substr_count($value, '.') > 1) {
            return false;
        }
    } else {
        $value = strval($value);
    }

    $length = strlen($value);

    return $length >= $parameters[0] && $length <= $parameters[1]
        && ctype_digit(str_replace('.', '', $value));
}

public function validateDigits($attribute, $value, $parameters)
{
    $this->requireParameterCount(1, $parameters, 'digits');

    // make sure that the value is a number or a numeric string
    if (!is_numeric($value)) {
        return false;
    }

    if (is_string($value)) {
        // make sure that the value is not a dot and does not contain more than single dot
        if ($value === '.' || substr_count($value, '.') > 1) {
            return false;
        }
    } else {
        $value = strval($value);
    }

    return strlen($value) == $parameters[0]
        && ctype_digit(str_replace('.', '', $value));
}

@taylorotwell taylorotwell merged commit 476f920 into 8.x May 10, 2022
@taylorotwell taylorotwell deleted the fix-multiple-dots-in-rule branch May 10, 2022 13:43
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