Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ee5d7fd

Browse files
committed
bug #30737 [Validator] Improve constraint default option check (vudaltsov)
This PR was squashed before being merged into the 3.4 branch (closes #30737). Discussion ---------- [Validator] Improve constraint default option check | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a Any constraint without default option used as annotation with unnamed first argument (for example, `@Assert\Collection(1)`) throws an exception with an ugly message `The options "" do not exist in constraint Collection`. This PR makes constraint check the default option in the annotation case in the same way it checks it in the "real" code case. So the exception will be `No default option is configured for constraint Collection.` Commits ------- 915912e [Validator] Improve constraint default option check
2 parents 2d00024 + 915912e commit ee5d7fd

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

src/Symfony/Component/Validator/Constraint.php

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,20 @@ public static function getErrorName($errorCode)
105105
*/
106106
public function __construct($options = null)
107107
{
108+
$defaultOption = $this->getDefaultOption();
108109
$invalidOptions = [];
109110
$missingOptions = array_flip((array) $this->getRequiredOptions());
110111
$knownOptions = get_object_vars($this);
111112

112113
// The "groups" option is added to the object lazily
113114
$knownOptions['groups'] = true;
114115

115-
if (\is_array($options) && \count($options) >= 1 && isset($options['value']) && !property_exists($this, 'value')) {
116-
$options[$this->getDefaultOption()] = $options['value'];
116+
if (\is_array($options) && isset($options['value']) && !property_exists($this, 'value')) {
117+
if (null === $defaultOption) {
118+
throw new ConstraintDefinitionException(sprintf('No default option is configured for constraint "%s".', \get_class($this)));
119+
}
120+
121+
$options[$defaultOption] = $options['value'];
117122
unset($options['value']);
118123
}
119124

@@ -130,26 +135,24 @@ public function __construct($options = null)
130135
}
131136
}
132137
} elseif (null !== $options && !(\is_array($options) && 0 === \count($options))) {
133-
$option = $this->getDefaultOption();
134-
135-
if (null === $option) {
136-
throw new ConstraintDefinitionException(sprintf('No default option is configured for constraint %s', \get_class($this)));
138+
if (null === $defaultOption) {
139+
throw new ConstraintDefinitionException(sprintf('No default option is configured for constraint "%s".', \get_class($this)));
137140
}
138141

139-
if (\array_key_exists($option, $knownOptions)) {
140-
$this->$option = $options;
141-
unset($missingOptions[$option]);
142+
if (\array_key_exists($defaultOption, $knownOptions)) {
143+
$this->$defaultOption = $options;
144+
unset($missingOptions[$defaultOption]);
142145
} else {
143-
$invalidOptions[] = $option;
146+
$invalidOptions[] = $defaultOption;
144147
}
145148
}
146149

147150
if (\count($invalidOptions) > 0) {
148-
throw new InvalidOptionsException(sprintf('The options "%s" do not exist in constraint %s', implode('", "', $invalidOptions), \get_class($this)), $invalidOptions);
151+
throw new InvalidOptionsException(sprintf('The options "%s" do not exist in constraint "%s".', implode('", "', $invalidOptions), \get_class($this)), $invalidOptions);
149152
}
150153

151154
if (\count($missingOptions) > 0) {
152-
throw new MissingOptionsException(sprintf('The options "%s" must be set for constraint %s', implode('", "', array_keys($missingOptions)), \get_class($this)), array_keys($missingOptions));
155+
throw new MissingOptionsException(sprintf('The options "%s" must be set for constraint "%s".', implode('", "', array_keys($missingOptions)), \get_class($this)), array_keys($missingOptions));
153156
}
154157
}
155158

@@ -173,7 +176,7 @@ public function __set($option, $value)
173176
return;
174177
}
175178

176-
throw new InvalidOptionsException(sprintf('The option "%s" does not exist in constraint %s', $option, \get_class($this)), [$option]);
179+
throw new InvalidOptionsException(sprintf('The option "%s" does not exist in constraint "%s".', $option, \get_class($this)), [$option]);
177180
}
178181

179182
/**
@@ -199,7 +202,7 @@ public function __get($option)
199202
return $this->groups;
200203
}
201204

202-
throw new InvalidOptionsException(sprintf('The option "%s" does not exist in constraint %s', $option, \get_class($this)), [$option]);
205+
throw new InvalidOptionsException(sprintf('The option "%s" does not exist in constraint "%s".', $option, \get_class($this)), [$option]);
203206
}
204207

205208
/**

src/Symfony/Component/Validator/Tests/ConstraintTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ public function testOptionsAsDefaultOption()
225225

226226
/**
227227
* @expectedException \Symfony\Component\Validator\Exception\InvalidOptionsException
228-
* @expectedExceptionMessage The options "0", "5" do not exist
228+
* @expectedExceptionMessage The options "0", "5" do not exist in constraint "Symfony\Component\Validator\Tests\Fixtures\ConstraintA".
229229
*/
230230
public function testInvalidOptions()
231231
{
@@ -242,4 +242,13 @@ public function testOptionsWithInvalidInternalPointer()
242242

243243
$this->assertEquals('foo', $constraint->property1);
244244
}
245+
246+
/**
247+
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
248+
* @expectedExceptionMessage No default option is configured for constraint "Symfony\Component\Validator\Tests\Fixtures\ConstraintB".
249+
*/
250+
public function testAnnotationSetUndefinedDefaultOption()
251+
{
252+
new ConstraintB(['value' => 1]);
253+
}
245254
}

0 commit comments

Comments
 (0)