symfony: [validator] ctype validators do not respect integer values

Symfony version(s) affected: master

Description ctype_digit() excepts all numbers to be strings (like: “123”) and does not validate real integers (like: 123).

How to reproduce

$violations = Validation::createValidator()->validate(5, new Type('digit'));
foreach ($violations as $violation) {
    echo $violation . "\n";
}

Triggers the following violation.

5:
    This value should be of type digit. (code ba785a8c-82cb-4283-967c-3cf342181b40)

Possible Solution
Cast the value to string before validation.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 17 (14 by maintainers)

Most upvoted comments

I think it’s related to #35316 also (depending if integer is your actual domain type), but ultimately there’s no validation currently for is_int(0) || is_string('0') || is_positive_int || /^[1-9]\d*$/ i agree.

So @NaturalNumber(allowInt=true|false, allowZero=true|false) seems elegant 👍