8000 [Form] Fixed required value guessed by ValidatorTypeGuesser by webmozart · Pull Request #3821 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Fixed required value guessed by ValidatorTypeGuesser #3821

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG-2.1.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 Al A5CE l @@ -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