Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9ba90bf

Browse files
committed
fix guessing form types for DateTime types
1 parent 9660e6b commit 9ba90bf

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/Symfony/Component/Form/Extension/Validator/ValidatorTypeGuesser.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public function guessTypeForConstraint(Constraint $constraint)
9797
case 'long':
9898
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\IntegerType', [], Guess::MEDIUM_CONFIDENCE);
9999

100+
case \DateTime::class:
100101
case '\DateTime':
101102
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateType', [], Guess::MEDIUM_CONFIDENCE);
102103

src/Symfony/Component/Form/Tests/Extension/Validator/ValidatorTypeGuesserTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,17 @@
1212
namespace Symfony\Component\Form\Tests\Extension\Validator;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
16+
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
17+
use Symfony\Component\Form\Extension\Core\Type\DateType;
18+
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
19+
use Symfony\Component\Form\Extension\Core\Type\NumberType;
20+
use Symfony\Component\Form\Extension\Core\Type\TextType;
1521
use Symfony\Component\Form\Extension\Validator\ValidatorTypeGuesser;
1622
use Symfony\Component\Form\Guess\Guess;
23+
use Symfony\Component\Form\Guess\TypeGuess;
1724
use Symfony\Component\Form\Guess\ValueGuess;
25+
use Symfony\Component\Validator\Constraint;
1826
use Symfony\Component\Validator\Constraints\Email;
1927
use Symfony\Component\Validator\Constraints\IsTrue;
2028
use Symfony\Component\Validator\Constraints\Length;
@@ -59,6 +67,35 @@ protected function setUp()
5967
$this->guesser = new ValidatorTypeGuesser($this->metadataFactory);
6068
}
6169

70+
/**
71+
* @dataProvider guessTypeProvider
72+
*/
73+
public function testGuessType(Constraint $constraint, TypeGuess $guess)
74+
{
75+
$this->metadata->addPropertyConstraint(self::TEST_PROPERTY, $constraint);
76+
77+
$this->assertEquals($guess, $this->guesser->guessType(self::TEST_CLASS, self::TEST_PROPERTY));
78+
}
79+
80+
public function guessTypeProvider()
81+
{
82+
return [
83+
[new Type('array'), new TypeGuess(CollectionType::class, [], Guess::MEDIUM_CONFIDENCE)],
84+
[new Type('bool'), new TypeGuess(CheckboxType::class, [], Guess::MEDIUM_CONFIDENCE)],
85+
[new Type('boolean'), new TypeGuess(CheckboxType::class, [], Guess::MEDIUM_CONFIDENCE)],
86+
[new Type('double'), new TypeGuess(NumberType::class, [], Guess::MEDIUM_CONFIDENCE)],
87+
[new Type('float'), new TypeGuess(NumberType::class, [], Guess::MEDIUM_CONFIDENCE)],
88+
[new Type('numeric'), new TypeGuess(NumberType::class, [], Guess::MEDIUM_CONFIDENCE)],
89+
[new Type('real'), new TypeGuess(NumberType::class, [], Guess::MEDIUM_CONFIDENCE)],
90+
[new Type('int'), new TypeGuess(IntegerType::class, [], Guess::MEDIUM_CONFIDENCE)],
91+
[new Type('integer'), new TypeGuess(IntegerType::class, [], Guess::MEDIUM_CONFIDENCE)],
92+
[new Type('long'), new TypeGuess(IntegerType::class, [], Guess::MEDIUM_CONFIDENCE)],
93+
[new Type('string'), new TypeGuess(TextType::class, [], Guess::LOW_CONFIDENCE)],
94+
[new Type(\DateTime::class), new TypeGuess(DateType::class, [], Guess::MEDIUM_CONFIDENCE)],
95+
[new Type('\DateTime'), new TypeGuess(DateType::class, [], Guess::MEDIUM_CONFIDENCE)],
96+
];
97+
}
98+
6299
public function guessRequiredProvider()
63100
{
64101
return [

0 commit comments

Comments
 (0)