[Form] Fix min/max length guessing for numeric types (fix #3091)

This commit is contained in:
Victor Berchet 2012-03-19 23:57:21 +01:00
parent edac48a824
commit fc7c7f6458
3 changed files with 92 additions and 179 deletions

View File

@ -60,6 +60,8 @@ class DoctrineOrmTypeGuesser implements FormTypeGuesserInterface
return new TypeGuess('datetime', array(), Guess::HIGH_CONFIDENCE); return new TypeGuess('datetime', array(), Guess::HIGH_CONFIDENCE);
case 'date': case 'date':
return new TypeGuess('date', array(), Guess::HIGH_CONFIDENCE); return new TypeGuess('date', array(), Guess::HIGH_CONFIDENCE);
case 'time':
return new TypeGuess('time', array(), Guess::HIGH_CONFIDENCE);
case 'decimal': case 'decimal':
case 'float': case 'float':
return new TypeGuess('number', array(), Guess::MEDIUM_CONFIDENCE); return new TypeGuess('number', array(), Guess::MEDIUM_CONFIDENCE);
@ -71,8 +73,6 @@ class DoctrineOrmTypeGuesser implements FormTypeGuesserInterface
return new TypeGuess('text', array(), Guess::MEDIUM_CONFIDENCE); return new TypeGuess('text', array(), Guess::MEDIUM_CONFIDENCE);
case 'text': case 'text':
return new TypeGuess('textarea', array(), Guess::MEDIUM_CONFIDENCE); return new TypeGuess('textarea', array(), Guess::MEDIUM_CONFIDENCE);
case 'time':
return new TypeGuess('time', array(), Guess::HIGH_CONFIDENCE);
default: default:
return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE); return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE);
} }
@ -105,6 +105,10 @@ class DoctrineOrmTypeGuesser implements FormTypeGuesserInterface
if (isset($mapping['length'])) { if (isset($mapping['length'])) {
return new ValueGuess($mapping['length'], Guess::HIGH_CONFIDENCE); return new ValueGuess($mapping['length'], Guess::HIGH_CONFIDENCE);
} }
if (in_array($ret[0]->getTypeOfField($property), array('decimal', 'float'))) {
return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE);
}
} }
} }
@ -113,6 +117,14 @@ class DoctrineOrmTypeGuesser implements FormTypeGuesserInterface
*/ */
public function guessMinLength($class, $property) public function guessMinLength($class, $property)
{ {
$ret = $this->getMetadata($class);
if ($ret && $ret[0]->hasField($property) && !$ret[0]->hasAssociation($property)) {
$mapping = $ret[0]->getFieldMapping($property);
if (in_array($ret[0]->getTypeOfField($property), array('decimal', 'float'))) {
return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE);
}
}
} }
protected function getMetadata($class) protected function getMetadata($class)

View File

@ -89,150 +89,68 @@ class ValidatorTypeGuesser implements FormTypeGuesserInterface
switch ($constraint->type) { switch ($constraint->type) {
case 'boolean': case 'boolean':
case 'bool': case 'bool':
return new TypeGuess( return new TypeGuess('checkbox', array(), Guess::MEDIUM_CONFIDENCE);
'checkbox',
array(),
Guess::MEDIUM_CONFIDENCE
);
case 'double': case 'double':
case 'float': case 'float':
case 'numeric': case 'numeric':
case 'real': case 'real':
return new TypeGuess( return new TypeGuess('number', array(), Guess::MEDIUM_CONFIDENCE);
'number',
array(),
Guess::MEDIUM_CONFIDENCE
);
case 'integer': case 'integer':
case 'int': case 'int':
case 'long': case 'long':
return new TypeGuess( return new TypeGuess('integer', array(), Guess::MEDIUM_CONFIDENCE);
'integer',
array(),
Guess::MEDIUM_CONFIDENCE
);
case 'string':
return new TypeGuess(
'text',
array(),
Guess::LOW_CONFIDENCE
);
case '\DateTime': case '\DateTime':
return new TypeGuess( return new TypeGuess('date', array(), Guess::MEDIUM_CONFIDENCE);
'date',
array(), case 'string':
Guess::MEDIUM_CONFIDENCE return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE);
);
} }
break; break;
case 'Symfony\Component\Validator\Constraints\Country': case 'Symfony\Component\Validator\Constraints\Country':
return new TypeGuess( return new TypeGuess('country', array(), Guess::HIGH_CONFIDENCE);
'country',
array(),
Guess::HIGH_CONFIDENCE
);
case 'Symfony\Component\Validator\Constraints\Date': case 'Symfony\Component\Validator\Constraints\Date':
return new TypeGuess( return new TypeGuess('date', array('type' => 'string'), Guess::HIGH_CONFIDENCE);
'date',
array('type' => 'string'),
Guess::HIGH_CONFIDENCE
);
case 'Symfony\Component\Validator\Constraints\DateTime': case 'Symfony\Component\Validator\Constraints\DateTime':
return new TypeGuess( return new TypeGuess('datetime', array('type' => 'string'), Guess::HIGH_CONFIDENCE);
'datetime',
array('type' => 'string'),
Guess::HIGH_CONFIDENCE
);
case 'Symfony\Component\Validator\Constraints\Email': case 'Symfony\Component\Validator\Constraints\Email':
return new TypeGuess( return new TypeGuess('email', array(), Guess::HIGH_CONFIDENCE);
'email',
array(),
Guess::HIGH_CONFIDENCE
);
case 'Symfony\Component\Validator\Constraints\File': case 'Symfony\Component\Validator\Constraints\File':
return new TypeGuess(
'file',
array(),
Guess::HIGH_CONFIDENCE
);
case 'Symfony\Component\Validator\Constraints\Image': case 'Symfony\Component\Validator\Constraints\Image':
return new TypeGuess( return new TypeGuess('file', array(), Guess::HIGH_CONFIDENCE);
'file',
array(),
Guess::HIGH_CONFIDENCE
);
case 'Symfony\Component\Validator\Constraints\Ip':
return new TypeGuess(
'text',
array(),
Guess::MEDIUM_CONFIDENCE
);
case 'Symfony\Component\Validator\Constraints\Language': case 'Symfony\Component\Validator\Constraints\Language':
return new TypeGuess( return new TypeGuess('language', array(), Guess::HIGH_CONFIDENCE);
'language',
array(),
Guess::HIGH_CONFIDENCE
);
case 'Symfony\Component\Validator\Constraints\Locale': case 'Symfony\Component\Validator\Constraints\Locale':
return new TypeGuess( return new TypeGuess('locale', array(), Guess::HIGH_CONFIDENCE);
'locale',
array(),
Guess::HIGH_CONFIDENCE
);
case 'Symfony\Component\Validator\Constraints\Max':
return new TypeGuess(
'number',
array(),
Guess::LOW_CONFIDENCE
);
case 'Symfony\Component\Validator\Constraints\MaxLength':
return new TypeGuess(
'text',
array(),
Guess::LOW_CONFIDENCE
);
case 'Symfony\Component\Validator\Constraints\Min':
return new TypeGuess(
'number',
array(),
Guess::LOW_CONFIDENCE
);
case 'Symfony\Component\Validator\Constraints\MinLength':
return new TypeGuess(
'text',
array(),
Guess::LOW_CONFIDENCE
);
case 'Symfony\Component\Validator\Constraints\Regex':
return new TypeGuess(
'text',
array(),
Guess::LOW_CONFIDENCE
);
case 'Symfony\Component\Validator\Constraints\Time': case 'Symfony\Component\Validator\Constraints\Time':
return new TypeGuess( return new TypeGuess('time', array('type' => 'string'), Guess::HIGH_CONFIDENCE);
'time',
array('type' => 'string'),
Guess::HIGH_CONFIDENCE
);
case 'Symfony\Component\Validator\Constraints\Url': case 'Symfony\Component\Validator\Constraints\Url':
return new TypeGuess( return new TypeGuess('url', array(), Guess::HIGH_CONFIDENCE);
'url',
array(), case 'Symfony\Component\Validator\Constraints\Ip':
Guess::HIGH_CONFIDENCE return new TypeGuess('text', array(), Guess::MEDIUM_CONFIDENCE);
);
case 'Symfony\Component\Validator\Constraints\Size': case 'Symfony\Component\Validator\Constraints\MaxLength':
return new TypeGuess( case 'Symfony\Component\Validator\Constraints\MinLength':
'number', case 'Symfony\Component\Validator\Constraints\Regex':
array(),
Guess::LOW_CONFIDENCE
);
case 'Symfony\Component\Validator\Constraints\SizeLength': case 'Symfony\Component\Validator\Constraints\SizeLength':
return new TypeGuess( return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE);
'text',
array(), case 'Symfony\Component\Validator\Constraints\Min':
Guess::LOW_CONFIDENCE case 'Symfony\Component\Validator\Constraints\Size':
); case 'Symfony\Component\Validator\Constraints\Max':
return new TypeGuess('number', array(), Guess::LOW_CONFIDENCE);
} }
} }
@ -247,20 +165,11 @@ class ValidatorTypeGuesser implements FormTypeGuesserInterface
{ {
switch (get_class($constraint)) { switch (get_class($constraint)) {
case 'Symfony\Component\Validator\Constraints\NotNull': case 'Symfony\Component\Validator\Constraints\NotNull':
return new ValueGuess(
true,
Guess::HIGH_CONFIDENCE
);
case 'Symfony\Component\Validator\Constraints\NotBlank': case 'Symfony\Component\Validator\Constraints\NotBlank':
return new ValueGuess( return new ValueGuess(true, Guess::HIGH_CONFIDENCE);
true,
Guess::HIGH_CONFIDENCE
);
default: default:
return new ValueGuess( return new ValueGuess(false, Guess::LOW_CONFIDENCE);
false,
Guess::LOW_CONFIDENCE
);
} }
} }
@ -275,25 +184,21 @@ class ValidatorTypeGuesser implements FormTypeGuesserInterface
{ {
switch (get_class($constraint)) { switch (get_class($constraint)) {
case 'Symfony\Component\Validator\Constraints\MaxLength': case 'Symfony\Component\Validator\Constraints\MaxLength':
return new ValueGuess( return new ValueGuess($constraint->limit, Guess::HIGH_CONFIDENCE);
$constraint->limit,
Guess::HIGH_CONFIDENCE
);
case 'Symfony\Component\Validator\Constraints\Max':
return new ValueGuess(
strlen((string) $constraint->limit),
Guess::HIGH_CONFIDENCE
);
case 'Symfony\Component\Validator\Constraints\SizeLength': case 'Symfony\Component\Validator\Constraints\SizeLength':
return new ValueGuess( return new ValueGuess($constraint->max, Guess::HIGH_CONFIDENCE);
$constraint->max,
Guess::HIGH_CONFIDENCE case 'Symfony\Component\Validator\Constraints\Type':
); if (in_array($constraint->type, array('double', 'float', 'numeric', 'real'))) {
return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE);
}
case 'Symfony\Component\Validator\Constraints\Max':
return new ValueGuess(strlen((string) $constraint->limit), Guess::LOW_CONFIDENCE);
case 'Symfony\Component\Validator\Constraints\Size': case 'Symfony\Component\Validator\Constraints\Size':
return new ValueGuess( return new ValueGuess(strlen((string) $constraint->max), Guess::LOW_CONFIDENCE);
strlen((string) $constraint->max),
Guess::HIGH_CONFIDENCE
);
} }
} }
@ -308,25 +213,21 @@ class ValidatorTypeGuesser implements FormTypeGuesserInterface
{ {
switch (get_class($constraint)) { switch (get_class($constraint)) {
case 'Symfony\Component\Validator\Constraints\MinLength': case 'Symfony\Component\Validator\Constraints\MinLength':
return new ValueGuess( return new ValueGuess($constraint->limit, Guess::HIGH_CONFIDENCE);
$constraint->limit,
Guess::HIGH_CONFIDENCE
);
case 'Symfony\Component\Validator\Constraints\Min':
return new ValueGuess(
strlen((string) $constraint->limit),
Guess::HIGH_CONFIDENCE
);
case 'Symfony\Component\Validator\Constraints\SizeLength': case 'Symfony\Component\Validator\Constraints\SizeLength':
return new ValueGuess( return new ValueGuess($constraint->min, Guess::HIGH_CONFIDENCE);
$constraint->min,
Guess::HIGH_CONFIDENCE case 'Symfony\Component\Validator\Constraints\Type':
); if (in_array($constraint->type, array('double', 'float', 'numeric', 'real'))) {
return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE);
}
case 'Symfony\Component\Validator\Constraints\Min':
return new ValueGuess(strlen((string) $constraint->limit), Guess::LOW_CONFIDENCE);
case 'Symfony\Component\Validator\Constraints\Size': case 'Symfony\Component\Validator\Constraints\Size':
return new ValueGuess( return new ValueGuess(strlen((string) $constraint->min), Guess::LOW_CONFIDENCE);
strlen((string) $constraint->min),
Guess::HIGH_CONFIDENCE
);
} }
} }

View File

@ -349,16 +349,16 @@ class FormFactory implements FormFactoryInterface
$type = $typeGuess ? $typeGuess->getType() : 'text'; $type = $typeGuess ? $typeGuess->getType() : 'text';
if ($maxLengthGuess) { $maxLength = $maxLengthGuess ? $maxLengthGuess->getValue() : null;
$options = array_merge(array('max_length' => $maxLengthGuess->getValue()), $options); $minLength = $minLengthGuess ? $minLengthGuess->getValue() : null;
$minLength = $minLength ?: 0;
if (null !== $maxLength) {
$options = array_merge(array('max_length' => $maxLength), $options);
} }
if ($minLengthGuess) { if ($minLength > 0) {
if ($maxLengthGuess) { $options = array_merge(array('pattern' => '.{'.$minLength.','.$maxLength.'}'), $options);
$options = array_merge(array('pattern' => '.{'.$minLengthGuess->getValue().','.$maxLengthGuess->getValue().'}'), $options);
} else {
$options = array_merge(array('pattern' => '.{'.$minLengthGuess->getValue().',}'), $options);
}
} }
if ($requiredGuess) { if ($requiredGuess) {