Skip to content

Commit e0ce6b4

Browse files
committed
[Form] Fixed required value guessed by ValidatorTypeGuesser
1 parent 13aa515 commit e0ce6b4

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

CHANGELOG-2.1.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,8 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c
260260
* [BC BREAK] FormType::getParent() does not see default options anymore
261261
* [BC BREAK] The methods `add`, `remove`, `setParent`, `bind` and `setData`
262262
in class Form now throw an exception if the form is already bound
263+
* fields of constrained classes without a NotBlank or NotNull constraint are
264+
set to not required now, as stated in the docs
263265

264266
### HttpFoundation
265267

src/Symfony/Component/Form/Extension/Validator/ValidatorTypeGuesser.php

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ public function guessRequired($class, $property)
4848

4949
return $this->guess($class, $property, function (Constraint $constraint) use ($guesser) {
5050
return $guesser->guessRequiredForConstraint($constraint);
51-
});
51+
// If we don't find any constraint telling otherwise, we can assume
52+
// that a field is not required (with LOW_CONFIDENCE)
53+
}, false);
5254
}
5355

5456
/**
@@ -167,9 +169,6 @@ public function guessRequiredForConstraint(Constraint $constraint)
167169
case 'Symfony\Component\Validator\Constraints\NotNull':
168170
case 'Symfony\Component\Validator\Constraints\NotBlank':
169171
return new ValueGuess(true, Guess::HIGH_CONFIDENCE);
170-
171-
default:
172-
return new ValueGuess(false, Guess::LOW_CONFIDENCE);
173172
}
174173
}
175174

@@ -221,7 +220,7 @@ public function guessMinLengthForConstraint(Constraint $constraint)
221220

222221
case 'Symfony\Component\Validator\Constraints\Type':
223222
if (in_array($constraint->type, array('double', 'float', 'numeric', 'real'))) {
224-
return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE);
223+
return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE);
225224
}
226225
break;
227226

@@ -237,13 +236,16 @@ public function guessMinLengthForConstraint(Constraint $constraint)
237236
* Iterates over the constraints of a property, executes a constraints on
238237
* them and returns the best guess
239238
*
240-
* @param string $class The class to read the constraints from
241-
* @param string $property The property for which to find constraints
242-
* @param \Closure $guessForConstraint The closure that returns a guess
243-
* for a given constraint
239+
* @param string $class The class to read the constraints from
240+
* @param string $property The property for which to find constraints
241+
* @param \Closure $closure The closure that returns a guess
242+
* for a given constraint
243+
* @param mixed $default The default value assumed if no other value
244+
* can be guessed.
245+
*
244246
* @return Guess The guessed value with the highest confidence
245247
*/
246-
protected function guess($class, $property, \Closure $guessForConstraint)
248+
protected function guess($class, $property, \Closure $closure, $defaultValue = null)
247249
{
248250
$guesses = array();
249251
$classMetadata = $this->metadataFactory->getClassMetadata($class);
@@ -255,11 +257,15 @@ protected function guess($class, $property, \Closure $guessForConstraint)
255257
$constraints = $memberMetadata->getConstraints();
256258

257259
foreach ($constraints as $constraint) {
258-
if ($guess = $guessForConstraint($constraint)) {
260+
if ($guess = $closure($constraint)) {
259261
$guesses[] = $guess;
260262
}
261263
}
262264
}
265+
266+
if (null !== $defaultValue) {
267+
$guesses[] = new ValueGuess($defaultValue, Guess::LOW_CONFIDENCE);
268+
}
263269
}
264270

265271
return Guess::getBestGuess($guesses);

0 commit comments

Comments
 (0)
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