merged branch bschussek/issue3661 (PR #3821)
Commits
-------
e0ce6b4
[Form] Fixed required value guessed by ValidatorTypeGuesser
Discussion
----------
[Form] Fixed required value guessed by ValidatorTypeGuesser
Bug fix: yes
Feature addition: no
Backwards compatibility break: no*
Symfony2 tests pass: yes
Fixes the following tickets: #3661
Todo: -
![Travis Build Status](https://secure.travis-ci.org/bschussek/symfony.png?branch=issue3661)
The documentation states that a field is set to required if constrained with NotBlank or NotNull and to not required otherwise. Due to a bug this was not the case and fields were always required. This is now fixed.
The consequence is that some fields that were required before by default are not required anymore. The only difference is that the HTML5 "required" attribute disappears, because server-side validation did not occur before anyway.
This commit is contained in:
commit
34b1b6b17d
@ -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
|
||||
|
||||
|
@ -48,7 +48,9 @@ class ValidatorTypeGuesser implements FormTypeGuesserInterface
|
||||
|
||||
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 @@ class ValidatorTypeGuesser implements FormTypeGuesserInterface
|
||||
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 @@ class ValidatorTypeGuesser implements FormTypeGuesserInterface
|
||||
|
||||
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 @@ class ValidatorTypeGuesser implements FormTypeGuesserInterface
|
||||
* 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 @@ class ValidatorTypeGuesser implements FormTypeGuesserInterface
|
||||
$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);
|
||||
|
Reference in New Issue
Block a user