Skip to content

[Form] Restored and deprecated method guessMinLength in FormTypeGuesser #4085

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 23, 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
1 change: 1 addition & 0 deletions CHANGELOG-2.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c
"input"
* ValidatorTypeGuesser now guesses "collection" for array type constraint
* added method `guessPattern` to FormTypeGuesserInterface to guess which pattern to use in the HTML5 attribute "pattern"
* deprecated method `guessMinLength` in favor of `guessPattern`

### HttpFoundation

Expand Down
23 changes: 23 additions & 0 deletions UPGRADE-2.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,29 @@

* [BC BREAK] renamed "field_*" theme blocks to "form_*" and "field_widget" to
"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

Expand Down
7 changes: 7 additions & 0 deletions src/Symfony/Bridge/Doctrine/Form/DoctrineOrmTypeGuesser.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ public function guessMaxLength($class, $property)
}
}

/**
* {@inheritDoc}
*/
public function guessMinLength($class, $property)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be raise an E_USER_DEPRECATED for deprecated methods?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no as we catch PHP notices to convert them to exceptions... except if we modify the error handling we have right now. But I'm not a big fan of PHP notices/warnings anyway as people don't really read them.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be useful to show people in the profiler which deprecated methods they are using.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. Maybe add special handling for E_USER_DEPRECATED?

{
}

/**
* {@inheritDoc}
*/
Expand Down
7 changes: 7 additions & 0 deletions src/Symfony/Bridge/Propel1/Form/PropelTypeGuesser.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ public function guessMaxLength($class, $property)
}
}

/**
* {@inheritDoc}
*/
public function guessMinLength($class, $property)
{
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ public function guessMaxLength($class, $property)
});
}

/**
* {@inheritDoc}
*/
public function guessMinLength($class, $property)
{
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -206,7 +213,7 @@ public function guessMaxLengthForConstraint(Constraint $constraint)

/**
* Guesses a field's pattern based on the given constraint
*
*
* @param Constraint $constraint The constraint to guess for
*
* @return Guess The guess for the pattern
Expand All @@ -228,7 +235,7 @@ public function guessPatternForConstraint(Constraint $constraint)

case 'Symfony\Component\Validator\Constraints\Size':
return new ValueGuess(sprintf('.{%s,%s}', strlen((string) $constraint->min), strlen((string) $constraint->max)), Guess::LOW_CONFIDENCE);

case 'Symfony\Component\Validator\Constraints\Type':
if (in_array($constraint->type, array('double', 'float', 'numeric', 'real'))) {
return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE);
Expand Down
16 changes: 12 additions & 4 deletions src/Symfony/Component/Form/FormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,24 +327,32 @@ public function createBuilderForProperty($class, $property, $data = null, array

$typeGuess = $this->guesser->guessType($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);
$patternGuess = $this->guesser->guessPattern($class, $property);

$type = $typeGuess ? $typeGuess->getType() : 'text';

$maxLength = $maxLengthGuess ? $maxLengthGuess->getValue() : null;
$minLength = $minLengthGuess ? $minLengthGuess->getValue() : null;
$pattern = $patternGuess ? $patternGuess->getValue() : null;

// overrides $minLength, if set
if (null !== $pattern) {
$options = array_merge(array('pattern' => $pattern), $options);
}

if (null !== $maxLength) {
$options = array_merge(array('max_length' => $maxLength), $options);
}

if ($requiredGuess) {
$options = array_merge(array('required' => $requiredGuess->getValue()), $options);
if (null !== $minLength && $minLength > 0) {
$options = array_merge(array('pattern' => '.{'.$minLength.','.$maxLength.'}'), $options);
}

if (null !== $pattern) {
$options = array_merge(array('pattern' => $pattern), $options);
if ($requiredGuess) {
$options = array_merge(array('required' => $requiredGuess->getValue()), $options);
}

// user options may override guessed options
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/Form/FormTypeGuesserChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ public function guessMaxLength($class, $property)
});
}

/**
* {@inheritDoc}
*/
public function guessMinLength($class, $property)
{
return $this->guess(function ($guesser) use ($class, $property) {
return $guesser->guessMinLength($class, $property);
});
}

/**
* {@inheritDoc}
*/
Expand Down
14 changes: 13 additions & 1 deletion src/Symfony/Component/Form/FormTypeGuesserInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,21 @@ function guessRequired($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
*
*
* - 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.
* Example:
Expand Down
68 changes: 68 additions & 0 deletions src/Symfony/Component/Form/Tests/FormFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,74 @@ public function testCreateBuilderUsesMaxLengthIfFound()
$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()
{
$this->guesser1->expects($this->once())
Expand Down