Skip to content

Commit

Permalink
[Form] Fixed required value guessed by ValidatorTypeGuesser
Browse files Browse the repository at this point in the history
  • Loading branch information
webmozart committed Apr 7, 2012
1 parent 13aa515 commit e0ce6b4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG-2.1.md
Expand Up @@ -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

Expand Down
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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;

Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit e0ce6b4

Please sign in to comment.