Skip to content

Invalid GreaterThan constraint validation #45710

Closed as not planned
Closed as not planned
@astralo

Description

@astralo

Symfony version(s) affected

5.4.6

Description

Problem that brokes compatibility because of comparison difference of string and integer

symfony/validator:5.4.6 and other

composer.json conains php dependency as
"php": ">=7.2.5",

./Constraints/GreaterThanValidator.php:27
contains comparison line like $value1 > $value2

PHP version Condition Result
7.4.28 "1234asd" > 0 true
7.4.28 "1234" > 0 true
7.4.28 "asd" > 0 true
8.0.0 "1234asd" > 0 true
8.0.0 "1234" > 0 true
8.0.0 "asd" > 0 false

How to reproduce

add validation script

use Symfony\Component\Validator\Validator\ValidatorInterface;

$container = new Container();
$validator = $container->get(ValidatorInterface::class)
$errors = $validator->validate("string", [
  new Positive(),
]);

// for PHP 7.4
echo $errors->count() // => 1

// for PHP 8.0
echo $errors->count() // => 0

Possible Solution

There are several possible solutions

  1. change php version in composer.json
    from "php": ">=7.2.5" to "php": "^7.2.5" - restrict to use this with php 8.0 and below

  2. change ./Constraints/GreaterThanValidator.php:27 comparison (recommended)
    from
    return null === $value2 || $value1 > $value2;
    to
    return null === $value2 || !is_numeric($value1) || $value1 > $value2;

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions