Skip to content

Commit

Permalink
bug #30736 [Validator] Fix annotation default for @count and @Length
Browse files Browse the repository at this point in the history
…(vudaltsov)

This PR was squashed before being merged into the 3.4 branch (closes #30736).

Discussion
----------

[Validator] Fix annotation default for @count and @Length

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | is it worth mentioning?

`Count` and `Length` constraints allow to pass the exact value as the constructor argument when used in code.

```php
new Length(5);
// is same as
new Length(['min' => 5, 'max' => 5]);
```

At the same time when using them as annotations, `@Assert\Length(5)` throws `The options "" do not exist in constraint Symfony\\Component\\Validator\\Constraints\\Length` (fix for ugly exception is proposed in #30737). This happens because annotation's default value is passed as `value`. Since `Length` does not have a default option, `value` is replaced with `''`.

This PR fixes this inconsistency.

Commits
-------

7bfb8c1 [Validator] Fix annotation default for @count and @Length
  • Loading branch information
fabpot committed Mar 29, 2019
2 parents c79e52a + 7bfb8c1 commit 2d00024
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 2d00024

Please sign in to comment.