Skip to content

Commit

Permalink
[Validator] Fix annotation default for @count and @Length
Browse files Browse the repository at this point in the history
  • Loading branch information
vudaltsov authored and fabpot committed Mar 29, 2019
1 parent c79e52a commit 7bfb8c1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Count.php
Expand Up @@ -43,6 +43,9 @@ public function __construct($options = null)
'min' => $options,
'max' => $options,
];
} elseif (\is_array($options) && isset($options['value']) && !isset($options['min']) && !isset($options['max'])) {
$options['min'] = $options['max'] = $options['value'];
unset($options['value']);
}

parent::__construct($options);
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Length.php
Expand Up @@ -47,6 +47,9 @@ public function __construct($options = null)
'min' => $options,
'max' => $options,
];
} elseif (\is_array($options) && isset($options['value']) && !isset($options['min']) && !isset($options['max'])) {
$options['min'] = $options['max'] = $options['value'];
unset($options['value']);
}

parent::__construct($options);
Expand Down
Expand Up @@ -195,4 +195,13 @@ public function testDefaultOption()
$this->assertEquals(5, $constraint->min);
$this->assertEquals(5, $constraint->max);
}

public function testConstraintAnnotationDefaultOption()
{
$constraint = new Count(['value' => 5, 'exactMessage' => 'message']);

$this->assertEquals(5, $constraint->min);
$this->assertEquals(5, $constraint->max);
$this->assertEquals('message', $constraint->exactMessage);
}
}
Expand Up @@ -237,11 +237,20 @@ public function testOneCharset($value, $charset, $isValid)
}
}

public function testConstraintGetDefaultOption()
public function testConstraintDefaultOption()
{
$constraint = new Length(5);

$this->assertEquals(5, $constraint->min);
$this->assertEquals(5, $constraint->max);
}

public function testConstraintAnnotationDefaultOption()
{
$constraint = new Length(['value' => 5, 'exactMessage' => 'message']);

$this->assertEquals(5, $constraint->min);
$this->assertEquals(5, $constraint->max);
$this->assertEquals('message', $constraint->exactMessage);
}
}

0 comments on commit 7bfb8c1

Please sign in to comment.