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

Skip to content

Commit 40f2512

Browse files
fancywebBernhard Schussek
authored and
Bernhard Schussek
committed
[DoctrineBridge] Add decimal form type
1 parent 5fe3701 commit 40f2512

File tree

4 files changed

+191
-0
lines changed

4 files changed

+191
-0
lines changed

src/Symfony/Bridge/Doctrine/Form/DoctrineOrmTypeGuesser.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Doctrine\DBAL\Types\Type;
1818
use Doctrine\ORM\Mapping\ClassMetadataInfo;
1919
use Doctrine\ORM\Mapping\MappingException as LegacyMappingException;
20+
use Symfony\Bridge\Doctrine\Form\Type\DecimalType;
2021
use Symfony\Component\Form\FormTypeGuesserInterface;
2122
use Symfony\Component\Form\Guess\Guess;
2223
use Symfony\Component\Form\Guess\TypeGuess;
@@ -75,6 +76,7 @@ public function guessType($class, $property)
7576
case 'time_immutable':
7677
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\TimeType', ['input' => 'datetime_immutable'], Guess::HIGH_CONFIDENCE);
7778
case Type::DECIMAL:
79+
return new TypeGuess(DecimalType::class, array(), Guess::HIGH_CONFIDENCE);
7880
case Type::FLOAT:
7981
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\NumberType', [], Guess::MEDIUM_CONFIDENCE);
8082
case Type::INTEGER:
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Form\Type;
13+
14+
use Symfony\Component\Form\AbstractType;
15+
use Symfony\Component\Form\CallbackTransformer;
16+
use Symfony\Component\Form\Exception\TransformationFailedException;
17+
use Symfony\Component\Form\Extension\Core\Type\NumberType;
18+
use Symfony\Component\Form\FormBuilderInterface;
19+
20+
class DecimalType extends AbstractType
21+
{
22+
/**
23+
* {@inheritdoc}
24+
*/
25+
public function buildForm(FormBuilderInterface $builder, array $options)
26+
{
27+
$builder->addModelTransformer(new CallbackTransformer(function ($value) {
28+
if (null === $value) {
29+
return null;
30+
}
31+
32+
if (!is_string($value)) {
33+
throw new TransformationFailedException('Expected a string.');
34+
}
35+
36+
return $value;
37+
}, function ($value) {
38+
if (null === $value) {
39+
return null;
40+
}
41+
42+
if (!is_int($value) && !is_float($value)) {
43+
throw new TransformationFailedException('Expected an int or a float.');
44+
}
45+
46+
return (string) $value;
47+
}));
48+
}
49+
50+
/**
51+
* {@inheritdoc}
52+
*/
53+
public function getParent()
54+
{
55+
return NumberType::class;
56+
}
57+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Tests\Fixtures;
13+
14+
use Doctrine\ORM\Mapping\Entity;
15+
use Doctrine\ORM\Mapping\Id;
16+
use Doctrine\ORM\Mapping\Column;
17+
18+
/** @Entity */
19+
class Price
20+
{
21+
/** @Id @Column(type="integer") */
22+
public $id;
23+
24+
/** @Column(type="decimal") */
25+
public $value;
26+
27+
/**
28+
* @param int $id
29+
* @param float $value
30+
*/
31+
public function __construct(int $id, float $value)
32+
{
33+
$this->id = $id;
34+
$this->value = $value;
35+
}
36+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Tests\Form\Type;
13+
14+
use Doctrine\ORM\EntityManager;
15+
use Doctrine\ORM\Tools\SchemaTool;
16+
use Symfony\Bridge\Doctrine\Form\Type\DecimalType;
17+
use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper;
18+
use Symfony\Bridge\Doctrine\Tests\Fixtures\Price;
19+
use Symfony\Component\Form\Extension\Core\Type\FormType;
20+
use Symfony\Component\Form\Tests\Extension\Core\Type\BaseTypeTest;
21+
22+
class DecimalTypeTest extends BaseTypeTest
23+
{
24+
/**
25+
* @var string
26+
*/
27+
const TESTED_TYPE = DecimalType::class;
28+
29+
/**
30+
* @var EntityManager
31+
*/
32+
private $em;
33+
34+
protected function setUp()
35+
{
36+
$this->em = DoctrineTestHelper::createTestEntityManager();
37+
38+
parent::setUp();
39+
40+
$schemaTool = new SchemaTool($this->em);
41+
$classes = array(
42+
$this->em->getClassMetadata(Price::class)
43+
);
44+
45+
try {
46+
$schemaTool->dropSchema($classes);
47+
} catch (\Exception $e) {
48+
}
49+
50+
try {
51+
$schemaTool->createSchema($classes);
52+
} catch (\Exception $e) {
53+
}
54+
}
55+
56+
protected function tearDown()
57+
{
58+
parent::tearDown();
59+
60+
$this->em = null;
61+
}
62+
63+
public function testSubmitWithSameStringValue()
64+
{
65+
$price = new Price(1, 1.23);
66+
$this->em->persist($price);
67+
$this->em->flush();
68+
69+
$this->em->refresh($price);
70+
71+
$this->assertInternalType('string', $price->value);
72+
$stringValue = $price->value;
73+
74+
$formBuilder = $this->factory->createBuilder(FormType::class, $price, array(
75+
'data_class' => Price::class
76+
));
77+
$formBuilder->add('value', static::TESTED_TYPE);
78+
79+
$form = $formBuilder->getForm();
80+
$form->submit(array(
81+
'value' => $stringValue
82+
));
83+
84+
$this->assertSame($stringValue, $price->value);
85+
86+
$unitOfWork = $this->em->getUnitOfWork();
87+
$unitOfWork->computeChangeSets();
88+
89+
$this->assertSame(array(), $unitOfWork->getEntityChangeSet($price));
90+
}
91+
92+
public function testSubmitNull($expected = null, $norm = null, $view = null)
93+
{
94+
parent::testSubmitNull($expected, $norm, '');
95+
}
96+
}

0 commit comments

Comments
 (0)