[Form] Restored and deprecated method guessMinLength
in FormTypeGuesser
This commit is contained in:
parent
b7189fd54a
commit
d9e142bd62
@ -280,6 +280,7 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c
|
|||||||
"input"
|
"input"
|
||||||
* ValidatorTypeGuesser now guesses "collection" for array type constraint
|
* ValidatorTypeGuesser now guesses "collection" for array type constraint
|
||||||
* added method `guessPattern` to FormTypeGuesserInterface to guess which pattern to use in the HTML5 attribute "pattern"
|
* added method `guessPattern` to FormTypeGuesserInterface to guess which pattern to use in the HTML5 attribute "pattern"
|
||||||
|
* deprecated method `guessMinLength` in favor of `guessPattern`
|
||||||
|
|
||||||
### HttpFoundation
|
### HttpFoundation
|
||||||
|
|
||||||
|
@ -372,6 +372,29 @@
|
|||||||
|
|
||||||
* [BC BREAK] renamed "field_*" theme blocks to "form_*" and "field_widget" to
|
* [BC BREAK] renamed "field_*" theme blocks to "form_*" and "field_widget" to
|
||||||
"input"
|
"input"
|
||||||
|
|
||||||
|
* The method `guessMinLength()` of FormTypeGuesserInterface was deprecated
|
||||||
|
and will be removed in Symfony 2.3. You should use the new method
|
||||||
|
`guessPattern()` instead which may return any regular expression that
|
||||||
|
is inserted in the HTML5 attribute "pattern".
|
||||||
|
|
||||||
|
Before:
|
||||||
|
|
||||||
|
public function guessMinLength($class, $property)
|
||||||
|
{
|
||||||
|
if (/* condition */) {
|
||||||
|
return new ValueGuess($minLength, Guess::LOW_CONFIDENCE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
After:
|
||||||
|
|
||||||
|
public function guessPattern($class, $property)
|
||||||
|
{
|
||||||
|
if (/* condition */) {
|
||||||
|
return new ValueGuess('.{' . $minLength . ',}', Guess::LOW_CONFIDENCE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
### Validator
|
### Validator
|
||||||
|
|
||||||
|
@ -112,6 +112,13 @@ class DoctrineOrmTypeGuesser implements FormTypeGuesserInterface
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function guessMinLength($class, $property)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
|
@ -121,6 +121,13 @@ class PropelTypeGuesser implements FormTypeGuesserInterface
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function guessMinLength($class, $property)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
|
@ -65,6 +65,13 @@ class ValidatorTypeGuesser implements FormTypeGuesserInterface
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function guessMinLength($class, $property)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
@ -206,7 +213,7 @@ class ValidatorTypeGuesser implements FormTypeGuesserInterface
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Guesses a field's pattern based on the given constraint
|
* Guesses a field's pattern based on the given constraint
|
||||||
*
|
*
|
||||||
* @param Constraint $constraint The constraint to guess for
|
* @param Constraint $constraint The constraint to guess for
|
||||||
*
|
*
|
||||||
* @return Guess The guess for the pattern
|
* @return Guess The guess for the pattern
|
||||||
@ -228,7 +235,7 @@ class ValidatorTypeGuesser implements FormTypeGuesserInterface
|
|||||||
|
|
||||||
case 'Symfony\Component\Validator\Constraints\Size':
|
case 'Symfony\Component\Validator\Constraints\Size':
|
||||||
return new ValueGuess(sprintf('.{%s,%s}', strlen((string) $constraint->min), strlen((string) $constraint->max)), Guess::LOW_CONFIDENCE);
|
return new ValueGuess(sprintf('.{%s,%s}', strlen((string) $constraint->min), strlen((string) $constraint->max)), Guess::LOW_CONFIDENCE);
|
||||||
|
|
||||||
case 'Symfony\Component\Validator\Constraints\Type':
|
case 'Symfony\Component\Validator\Constraints\Type':
|
||||||
if (in_array($constraint->type, array('double', 'float', 'numeric', 'real'))) {
|
if (in_array($constraint->type, array('double', 'float', 'numeric', 'real'))) {
|
||||||
return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE);
|
return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE);
|
||||||
|
@ -327,24 +327,32 @@ class FormFactory implements FormFactoryInterface
|
|||||||
|
|
||||||
$typeGuess = $this->guesser->guessType($class, $property);
|
$typeGuess = $this->guesser->guessType($class, $property);
|
||||||
$maxLengthGuess = $this->guesser->guessMaxLength($class, $property);
|
$maxLengthGuess = $this->guesser->guessMaxLength($class, $property);
|
||||||
|
// Keep $minLengthGuess for BC until Symfony 2.3
|
||||||
|
$minLengthGuess = $this->guesser->guessMinLength($class, $property);
|
||||||
$requiredGuess = $this->guesser->guessRequired($class, $property);
|
$requiredGuess = $this->guesser->guessRequired($class, $property);
|
||||||
$patternGuess = $this->guesser->guessPattern($class, $property);
|
$patternGuess = $this->guesser->guessPattern($class, $property);
|
||||||
|
|
||||||
$type = $typeGuess ? $typeGuess->getType() : 'text';
|
$type = $typeGuess ? $typeGuess->getType() : 'text';
|
||||||
|
|
||||||
$maxLength = $maxLengthGuess ? $maxLengthGuess->getValue() : null;
|
$maxLength = $maxLengthGuess ? $maxLengthGuess->getValue() : null;
|
||||||
|
$minLength = $minLengthGuess ? $minLengthGuess->getValue() : null;
|
||||||
$pattern = $patternGuess ? $patternGuess->getValue() : null;
|
$pattern = $patternGuess ? $patternGuess->getValue() : null;
|
||||||
|
|
||||||
|
// overrides $minLength, if set
|
||||||
|
if (null !== $pattern) {
|
||||||
|
$options = array_merge(array('pattern' => $pattern), $options);
|
||||||
|
}
|
||||||
|
|
||||||
if (null !== $maxLength) {
|
if (null !== $maxLength) {
|
||||||
$options = array_merge(array('max_length' => $maxLength), $options);
|
$options = array_merge(array('max_length' => $maxLength), $options);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($requiredGuess) {
|
if (null !== $minLength && $minLength > 0) {
|
||||||
$options = array_merge(array('required' => $requiredGuess->getValue()), $options);
|
$options = array_merge(array('pattern' => '.{'.$minLength.','.$maxLength.'}'), $options);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (null !== $pattern) {
|
if ($requiredGuess) {
|
||||||
$options = array_merge(array('pattern' => $pattern), $options);
|
$options = array_merge(array('required' => $requiredGuess->getValue()), $options);
|
||||||
}
|
}
|
||||||
|
|
||||||
// user options may override guessed options
|
// user options may override guessed options
|
||||||
|
@ -70,6 +70,16 @@ class FormTypeGuesserChain implements FormTypeGuesserInterface
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function guessMinLength($class, $property)
|
||||||
|
{
|
||||||
|
return $this->guess(function ($guesser) use ($class, $property) {
|
||||||
|
return $guesser->guessMinLength($class, $property);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
|
@ -43,9 +43,21 @@ interface FormTypeGuesserInterface
|
|||||||
*/
|
*/
|
||||||
function guessMaxLength($class, $property);
|
function guessMaxLength($class, $property);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a guess about the field's minimum length
|
||||||
|
*
|
||||||
|
* @param string $class The fully qualified class name
|
||||||
|
* @param string $property The name of the property to guess for
|
||||||
|
*
|
||||||
|
* @return Guess A guess for the field's minimum length
|
||||||
|
*
|
||||||
|
* @deprecated Deprecated since version 2.1, to be removed in 2.3.
|
||||||
|
*/
|
||||||
|
function guessMinLength($class, $property);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a guess about the field's pattern
|
* Returns a guess about the field's pattern
|
||||||
*
|
*
|
||||||
* - When you have a min value, you guess a min length of this min (LOW_CONFIDENCE) , lines below
|
* - When you have a min value, you guess a min length of this min (LOW_CONFIDENCE) , lines below
|
||||||
* - If this value is a float type, this is wrong so you guess null with MEDIUM_CONFIDENCE to override the previous guess.
|
* - If this value is a float type, this is wrong so you guess null with MEDIUM_CONFIDENCE to override the previous guess.
|
||||||
* Example:
|
* Example:
|
||||||
|
@ -458,6 +458,74 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals('builderInstance', $builder);
|
$this->assertEquals('builderInstance', $builder);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testCreateBuilderUsesMinLengthIfFound()
|
||||||
|
{
|
||||||
|
$this->guesser1->expects($this->once())
|
||||||
|
->method('guessMinLength')
|
||||||
|
->with('Application\Author', 'firstName')
|
||||||
|
->will($this->returnValue(new ValueGuess(
|
||||||
|
2,
|
||||||
|
Guess::MEDIUM_CONFIDENCE
|
||||||
|
)));
|
||||||
|
|
||||||
|
$this->guesser2->expects($this->once())
|
||||||
|
->method('guessMinLength')
|
||||||
|
->with('Application\Author', 'firstName')
|
||||||
|
->will($this->returnValue(new ValueGuess(
|
||||||
|
5,
|
||||||
|
Guess::HIGH_CONFIDENCE
|
||||||
|
)));
|
||||||
|
|
||||||
|
$factory = $this->createMockFactory(array('createNamedBuilder'));
|
||||||
|
|
||||||
|
$factory->expects($this->once())
|
||||||
|
->method('createNamedBuilder')
|
||||||
|
->with('text', 'firstName', null, array('pattern' => '.{5,}'))
|
||||||
|
->will($this->returnValue('builderInstance'));
|
||||||
|
|
||||||
|
$builder = $factory->createBuilderForProperty(
|
||||||
|
'Application\Author',
|
||||||
|
'firstName'
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals('builderInstance', $builder);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCreateBuilderPrefersPatternOverMinLength()
|
||||||
|
{
|
||||||
|
// min length is deprecated
|
||||||
|
$this->guesser1->expects($this->once())
|
||||||
|
->method('guessMinLength')
|
||||||
|
->with('Application\Author', 'firstName')
|
||||||
|
->will($this->returnValue(new ValueGuess(
|
||||||
|
2,
|
||||||
|
Guess::HIGH_CONFIDENCE
|
||||||
|
)));
|
||||||
|
|
||||||
|
// pattern is preferred even though confidence is lower
|
||||||
|
$this->guesser2->expects($this->once())
|
||||||
|
->method('guessPattern')
|
||||||
|
->with('Application\Author', 'firstName')
|
||||||
|
->will($this->returnValue(new ValueGuess(
|
||||||
|
'.{5,10}',
|
||||||
|
Guess::LOW_CONFIDENCE
|
||||||
|
)));
|
||||||
|
|
||||||
|
$factory = $this->createMockFactory(array('createNamedBuilder'));
|
||||||
|
|
||||||
|
$factory->expects($this->once())
|
||||||
|
->method('createNamedBuilder')
|
||||||
|
->with('text', 'firstName', null, array('pattern' => '.{5,10}'))
|
||||||
|
->will($this->returnValue('builderInstance'));
|
||||||
|
|
||||||
|
$builder = $factory->createBuilderForProperty(
|
||||||
|
'Application\Author',
|
||||||
|
'firstName'
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals('builderInstance', $builder);
|
||||||
|
}
|
||||||
|
|
||||||
public function testCreateBuilderUsesRequiredSettingWithHighestConfidence()
|
public function testCreateBuilderUsesRequiredSettingWithHighestConfidence()
|
||||||
{
|
{
|
||||||
$this->guesser1->expects($this->once())
|
$this->guesser1->expects($this->once())
|
||||||
|
Reference in New Issue
Block a user