diff --git a/CHANGELOG-2.1.md b/CHANGELOG-2.1.md index 6bbec4b0937f..7fe06840610e 100644 --- a/CHANGELOG-2.1.md +++ b/CHANGELOG-2.1.md @@ -260,6 +260,8 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c * [BC BREAK] FormType::getParent() does not see default options anymore * [BC BREAK] The methods `add`, `remove`, `setParent`, `bind` and `setData` in class Form now throw an exception if the form is already bound + * fields of constrained classes without a NotBlank or NotNull constraint are + set to not required now, as stated in the docs ### HttpFoundation diff --git a/src/Symfony/Component/Form/Extension/Validator/ValidatorTypeGuesser.php b/src/Symfony/Component/Form/Extension/Validator/ValidatorTypeGuesser.php index 7ffb4947ccd7..c3edc37af0bb 100644 --- a/src/Symfony/Component/Form/Extension/Validator/ValidatorTypeGuesser.php +++ b/src/Symfony/Component/Form/Extension/Validator/ValidatorTypeGuesser.php @@ -48,7 +48,9 @@ public function guessRequired($class, $property) return $this->guess($class, $property, function (Constraint $constraint) use ($guesser) { return $guesser->guessRequiredForConstraint($constraint); - }); + // If we don't find any constraint telling otherwise, we can assume + // that a field is not required (with LOW_CONFIDENCE) + }, false); } /** @@ -167,9 +169,6 @@ public function guessRequiredForConstraint(Constraint $constraint) case 'Symfony\Component\Validator\Constraints\NotNull': case 'Symfony\Component\Validator\Constraints\NotBlank': return new ValueGuess(true, Guess::HIGH_CONFIDENCE); - - default: - return new ValueGuess(false, Guess::LOW_CONFIDENCE); } } @@ -221,7 +220,7 @@ public function guessMinLengthForConstraint(Constraint $constraint) case 'Symfony\Component\Validator\Constraints\Type': if (in_array($constraint->type, array('double', 'float', 'numeric', 'real'))) { - return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE); + return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE); } break; @@ -237,13 +236,16 @@ public function guessMinLengthForConstraint(Constraint $constraint) * Iterates over the constraints of a property, executes a constraints on * them and returns the best guess * - * @param string $class The class to read the constraints from - * @param string $property The property for which to find constraints - * @param \Closure $guessForConstraint The closure that returns a guess - * for a given constraint + * @param string $class The class to read the constraints from + * @param string $property The property for which to find constraints + * @param \Closure $closure The closure that returns a guess + * for a given constraint + * @param mixed $default The default value assumed if no other value + * can be guessed. + * * @return Guess The guessed value with the highest confidence */ - protected function guess($class, $property, \Closure $guessForConstraint) + protected function guess($class, $property, \Closure $closure, $defaultValue = null) { $guesses = array(); $classMetadata = $this->metadataFactory->getClassMetadata($class); @@ -255,11 +257,15 @@ protected function guess($class, $property, \Closure $guessForConstraint) $constraints = $memberMetadata->getConstraints(); foreach ($constraints as $constraint) { - if ($guess = $guessForConstraint($constraint)) { + if ($guess = $closure($constraint)) { $guesses[] = $guess; } } } + + if (null !== $defaultValue) { + $guesses[] = new ValueGuess($defaultValue, Guess::LOW_CONFIDENCE); + } } return Guess::getBestGuess($guesses);