diff --git a/src/Symfony/Component/Validator/Constraint.php b/src/Symfony/Component/Validator/Constraint.php index d7f10bb37e288..3f8b803f78972 100644 --- a/src/Symfony/Component/Validator/Constraint.php +++ b/src/Symfony/Component/Validator/Constraint.php @@ -105,6 +105,7 @@ public static function getErrorName($errorCode) */ public function __construct($options = null) { + $defaultOption = $this->getDefaultOption(); $invalidOptions = []; $missingOptions = array_flip((array) $this->getRequiredOptions()); $knownOptions = get_object_vars($this); @@ -112,8 +113,12 @@ public function __construct($options = null) // The "groups" option is added to the object lazily $knownOptions['groups'] = true; - if (\is_array($options) && \count($options) >= 1 && isset($options['value']) && !property_exists($this, 'value')) { - $options[$this->getDefaultOption()] = $options['value']; + if (\is_array($options) && isset($options['value']) && !property_exists($this, 'value')) { + if (null === $defaultOption) { + throw new ConstraintDefinitionException(sprintf('No default option is configured for constraint "%s".', \get_class($this))); + } + + $options[$defaultOption] = $options['value']; unset($options['value']); } @@ -130,26 +135,24 @@ public function __construct($options = null) } } } elseif (null !== $options && !(\is_array($options) && 0 === \count($options))) { - $option = $this->getDefaultOption(); - - if (null === $option) { - throw new ConstraintDefinitionException(sprintf('No default option is configured for constraint %s', \get_class($this))); + if (null === $defaultOption) { + throw new ConstraintDefinitionException(sprintf('No default option is configured for constraint "%s".', \get_class($this))); } - if (\array_key_exists($option, $knownOptions)) { - $this->$option = $options; - unset($missingOptions[$option]); + if (\array_key_exists($defaultOption, $knownOptions)) { + $this->$defaultOption = $options; + unset($missingOptions[$defaultOption]); } else { - $invalidOptions[] = $option; + $invalidOptions[] = $defaultOption; } } if (\count($invalidOptions) > 0) { - throw new InvalidOptionsException(sprintf('The options "%s" do not exist in constraint %s', implode('", "', $invalidOptions), \get_class($this)), $invalidOptions); + throw new InvalidOptionsException(sprintf('The options "%s" do not exist in constraint "%s".', implode('", "', $invalidOptions), \get_class($this)), $invalidOptions); } if (\count($missingOptions) > 0) { - throw new MissingOptionsException(sprintf('The options "%s" must be set for constraint %s', implode('", "', array_keys($missingOptions)), \get_class($this)), array_keys($missingOptions)); + throw new MissingOptionsException(sprintf('The options "%s" must be set for constraint "%s".', implode('", "', array_keys($missingOptions)), \get_class($this)), array_keys($missingOptions)); } } @@ -173,7 +176,7 @@ public function __set($option, $value) return; } - throw new InvalidOptionsException(sprintf('The option "%s" does not exist in constraint %s', $option, \get_class($this)), [$option]); + throw new InvalidOptionsException(sprintf('The option "%s" does not exist in constraint "%s".', $option, \get_class($this)), [$option]); } /** @@ -199,7 +202,7 @@ public function __get($option) return $this->groups; } - throw new InvalidOptionsException(sprintf('The option "%s" does not exist in constraint %s', $option, \get_class($this)), [$option]); + throw new InvalidOptionsException(sprintf('The option "%s" does not exist in constraint "%s".', $option, \get_class($this)), [$option]); } /** diff --git a/src/Symfony/Component/Validator/Tests/ConstraintTest.php b/src/Symfony/Component/Validator/Tests/ConstraintTest.php index 87bac5b0a6e93..d1f41ce5ee97d 100644 --- a/src/Symfony/Component/Validator/Tests/ConstraintTest.php +++ b/src/Symfony/Component/Validator/Tests/ConstraintTest.php @@ -225,7 +225,7 @@ public function testOptionsAsDefaultOption() /** * @expectedException \Symfony\Component\Validator\Exception\InvalidOptionsException - * @expectedExceptionMessage The options "0", "5" do not exist + * @expectedExceptionMessage The options "0", "5" do not exist in constraint "Symfony\Component\Validator\Tests\Fixtures\ConstraintA". */ public function testInvalidOptions() { @@ -242,4 +242,13 @@ public function testOptionsWithInvalidInternalPointer() $this->assertEquals('foo', $constraint->property1); } + + /** + * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException + * @expectedExceptionMessage No default option is configured for constraint "Symfony\Component\Validator\Tests\Fixtures\ConstraintB". + */ + public function testAnnotationSetUndefinedDefaultOption() + { + new ConstraintB(['value' => 1]); + } } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy