Skip to content
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

[Form] add guess pattern #4077

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG-2.1.md
Expand Up @@ -277,6 +277,7 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c
* [BC BREAK] renamed "field_*" theme blocks to "form_*" and "field_widget" to
"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"
Copy link
Contributor

Choose a reason for hiding this comment

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

why is dropping guessMinLength() not noted as a BC break?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe i can make a PR to fixe this ?


### HttpFoundation

Expand Down
Expand Up @@ -115,7 +115,7 @@ public function guessMaxLength($class, $property)
/**
* {@inheritDoc}
*/
public function guessMinLength($class, $property)
public function guessPattern($class, $property)
{
$ret = $this->getMetadata($class);
if ($ret && $ret[0]->hasField($property) && !$ret[0]->hasAssociation($property)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/Propel1/Form/PropelTypeGuesser.php
Expand Up @@ -124,7 +124,7 @@ public function guessMaxLength($class, $property)
/**
* {@inheritDoc}
*/
public function guessMinLength($class, $property)
public function guessPattern($class, $property)
{
if ($column = $this->getColumn($class, $property)) {
switch ($column->getType()) {
Expand Down
Expand Up @@ -46,14 +46,14 @@ public function testGuessMaxLengthWithFloat()

public function testGuessMinLengthWithText()
{
$value = $this->guesser->guessMinLength(self::CLASS_NAME, 'value');
$value = $this->guesser->guessPattern(self::CLASS_NAME, 'value');

$this->assertNull($value);
}

public function testGuessMinLengthWithFloat()
{
$value = $this->guesser->guessMinLength(self::CLASS_NAME, 'price');
$value = $this->guesser->guessPattern(self::CLASS_NAME, 'price');

$this->assertNotNull($value);
$this->assertNull($value->getValue());
Expand Down
31 changes: 17 additions & 14 deletions src/Symfony/Component/Form/Extension/Validator/ValidatorTypeGuesser.php 100644 → 100755
Expand Up @@ -68,12 +68,12 @@ public function guessMaxLength($class, $property)
/**
* {@inheritDoc}
*/
public function guessMinLength($class, $property)
public function guessPattern($class, $property)
{
$guesser = $this;

return $this->guess($class, $property, function (Constraint $constraint) use ($guesser) {
return $guesser->guessMinLengthForConstraint($constraint);
return $guesser->guessPatternForConstraint($constraint);
});
}

Expand Down Expand Up @@ -205,32 +205,35 @@ public function guessMaxLengthForConstraint(Constraint $constraint)
}

/**
* Guesses a field's minimum length based on the given 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 minimum length
* @return Guess The guess for the pattern
*/
public function guessMinLengthForConstraint(Constraint $constraint)
public function guessPatternForConstraint(Constraint $constraint)
{
switch (get_class($constraint)) {
case 'Symfony\Component\Validator\Constraints\MinLength':
return new ValueGuess($constraint->limit, Guess::HIGH_CONFIDENCE);
return new ValueGuess(sprintf('.{%s,}', (string) $constraint->limit), Guess::LOW_CONFIDENCE);

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

case 'Symfony\Component\Validator\Constraints\Regex':
return new ValueGuess($constraint->pattern, Guess::HIGH_CONFIDENCE );

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

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);
}
break;

case 'Symfony\Component\Validator\Constraints\Min':
return new ValueGuess(strlen((string) $constraint->limit), Guess::LOW_CONFIDENCE);

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

Expand Down
15 changes: 7 additions & 8 deletions src/Symfony/Component/Form/FormFactory.php
Expand Up @@ -306,7 +306,7 @@ public function createNamedBuilder($type, $name, $data = null, array $options =
/**
* Returns a form builder for a property of a class.
*
* If any of the 'max_length', 'required' and type options can be guessed,
* If any of the 'max_length', 'required', 'pattern' and type options can be guessed,
* and are not provided in the options argument, the guessed value is used.
*
* @param string $class The fully qualified class name
Expand All @@ -327,27 +327,26 @@ public function createBuilderForProperty($class, $property, $data = null, array

$typeGuess = $this->guesser->guessType($class, $property);
$maxLengthGuess = $this->guesser->guessMaxLength($class, $property);
$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;
$minLength = $minLength ?: 0;
$pattern = $patternGuess ? $patternGuess->getValue() : null;

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

if ($minLength > 0) {
$options = array_merge(array('pattern' => '.{'.$minLength.','.$maxLength.'}'), $options);
}

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

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

// user options may override guessed options
if ($typeGuess) {
$options = array_merge($typeGuess->getOptions(), $options);
Expand Down
16 changes: 14 additions & 2 deletions src/Symfony/Component/Form/FormTypeGuesserChain.php
Expand Up @@ -40,31 +40,43 @@ public function __construct(array $guessers)
}
}

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

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

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

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

Expand Down
13 changes: 9 additions & 4 deletions src/Symfony/Component/Form/FormTypeGuesserInterface.php
Expand Up @@ -44,13 +44,18 @@ function guessRequired($class, $property);
function guessMaxLength($class, $property);

/**
* Returns a guess about the field's minimum length
* 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:
* You want a float greater than 5, 4.512313 is not valid but length(4.512314) > length(5)
* @link https://github.com/symfony/symfony/pull/3927
Copy link
Member

Choose a reason for hiding this comment

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

this description is very abstruse and the link doesn't belong here

*
* @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
* @return Guess A guess for the field's required pattern
*/
function guessMinLength($class, $property);

function guessPattern($class, $property);
}
57 changes: 45 additions & 12 deletions src/Symfony/Component/Form/Tests/FormFactoryTest.php
Expand Up @@ -491,6 +491,39 @@ public function testCreateBuilderUsesRequiredSettingWithHighestConfidence()
$this->assertEquals('builderInstance', $builder);
}

public function testCreateBuilderUsesPatternIfFound()
{
$this->guesser1->expects($this->once())
->method('guessPattern')
->with('Application\Author', 'firstName')
->will($this->returnValue(new ValueGuess(
'/[a-z]/',
Guess::MEDIUM_CONFIDENCE
)));

$this->guesser2->expects($this->once())
->method('guessPattern')
->with('Application\Author', 'firstName')
->will($this->returnValue(new ValueGuess(
'/[a-zA-Z]/',
Guess::HIGH_CONFIDENCE
)));

$factory = $this->createMockFactory(array('createNamedBuilder'));

$factory->expects($this->once())
->method('createNamedBuilder')
->with('text', 'firstName', null, array('pattern' => '/[a-zA-Z]/'))
->will($this->returnValue('builderInstance'));

$builder = $factory->createBuilderForProperty(
'Application\Author',
'firstName'
);

$this->assertEquals('builderInstance', $builder);
}

public function testCreateNamedBuilderFromParentBuilder()
{
$type = new FooType();
Expand Down Expand Up @@ -541,18 +574,18 @@ public function testUnknownOption()
$factory->createNamedBuilder($type, "text", "value", array("unknown" => "opt"));
}

public function testFormTypeCreatesDefaultValueForEmptyDataOption()
{
$factory = new FormFactory(array(new \Symfony\Component\Form\Extension\Core\CoreExtension()));
$form = $factory->createNamedBuilder(new AuthorType(), 'author')->getForm();
$form->bind(array('firstName' => 'John', 'lastName' => 'Smith'));
$author = new Author();
$author->firstName = 'John';
$author->setLastName('Smith');
$this->assertEquals($author, $form->getData());
public function testFieldTypeCreatesDefaultValueForEmptyDataOption()
{
$factory = new FormFactory(array(new \Symfony\Component\Form\Extension\Core\CoreExtension()));

$form = $factory->createNamedBuilder(new AuthorType(), 'author')->getForm();
$form->bind(array('firstName' => 'John', 'lastName' => 'Smith'));

$author = new Author();
$author->firstName = 'John';
$author->setLastName('Smith');

$this->assertEquals($author, $form->getData());
}

private function createMockFactory(array $methods = array())
Expand Down