Skip to content

Commit fd26a7e

Browse files
committed
[Form] Add range type - finished
[Form] Add range type #11979 - registered RangeType in CoreExtension [Form] Add range type #11979 - added tests for rendering the form field and validating options [Form] Add range type #11979 - added copyright lines in RangeTypeTest class [Form] Add range type #11979 - minor formatting fixes, added author block
1 parent 476eecb commit fd26a7e

File tree

4 files changed

+96
-7
lines changed

4 files changed

+96
-7
lines changed

src/Symfony/Component/Form/Extension/Core/CoreExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ protected function loadTypes()
5656
new Type\EmailType(),
5757
new Type\HiddenType(),
5858
new Type\IntegerType(),
59+
new Type\RangeType(),
5960
new Type\LanguageType(),
6061
new Type\LocaleType(),
6162
new Type\MoneyType(),

src/Symfony/Component/Form/Extension/Core/Type/RangeType.php

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,56 @@
1111

1212
namespace Symfony\Component\Form\Extension\Core\Type;
1313

14-
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
1514
use Symfony\Component\Form\AbstractType;
15+
use Symfony\Component\Form\FormInterface;
16+
use Symfony\Component\Form\FormView;
17+
use Symfony\Component\OptionsResolver\OptionsResolver;
1618

1719
/**
1820
* A range form element.
1921
*
2022
* @author Carlos Revillo <crevillo@gmail.com>
23+
* @author Pawel Krynicki <pawel.krynicki@hotmail.com>
2124
*/
2225
class RangeType extends AbstractType
2326
{
2427
/**
2528
* {@inheritdoc}
2629
*/
27-
public function setDefaultOptions(OptionsResolverInterface $resolver)
30+
public function buildView(FormView $view, FormInterface $form, array $options)
2831
{
29-
$resolver->setDefaults(array(
30-
'min' => null,
31-
'max' => null,
32-
));
32+
$view->vars['attr']['min'] = $options['min'];
33+
$view->vars['attr']['max'] = $options['max'];
34+
35+
if (isset($options['step'])) {
36+
$view->vars['attr']['step'] = $options['step'];
37+
}
38+
}
39+
40+
/**
41+
* {@inheritdoc}
42+
*/
43+
public function configureOptions(OptionsResolver $resolver)
44+
{
45+
$resolver->setRequired(array('min', 'max'));
46+
47+
$resolver->setDefined('step');
48+
49+
$resolver->setAllowedTypes('min', 'numeric');
50+
$resolver->setAllowedTypes('max', 'numeric');
51+
$resolver->setAllowedTypes('step', 'numeric');
52+
53+
$resolver->setAllowedValues('step', function ($value) {
54+
return $value > 0;
55+
});
3356
}
3457

3558
/**
3659
* {@inheritdoc}
3760
*/
3861
public function getParent()
3962
{
40-
return 'integer';
63+
return 'number';
4164
}
4265

4366
/**

src/Symfony/Component/Form/Tests/AbstractBootstrap3LayoutTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,6 +1388,27 @@ public function testInteger()
13881388
);
13891389
}
13901390

1391+
public function testRange()
1392+
{
1393+
$form = $this->factory->createNamed('name', 'range', 123.45, array(
1394+
'min' => 1.25,
1395+
'max' => 234.56,
1396+
'step' => 0.25,
1397+
));
1398+
1399+
$this->assertWidgetMatchesXpath($form->createView(), array('attr' => array('class' => 'my&class')),
1400+
'/input
1401+
[@type="range"]
1402+
[@name="name"]
1403+
[@min="1.25"]
1404+
[@max="234.56"]
1405+
[@step="0.25"]
1406+
[@class="my&class form-control"]
1407+
[@value="123.45"]
1408+
'
1409+
);
1410+
}
1411+
13911412
public function testLanguage()
13921413
{
13931414
$form = $this->factory->createNamed('name', 'language', 'de');
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Component\Form\Tests\Extension\Core\Type;
13+
14+
use Symfony\Component\Form\Test\TypeTestCase as TestCase;
15+
16+
/**
17+
* @author Pawel Krynicki <pawel.krynicki@hotmail.com>
18+
*/
19+
class RangeTypeTest extends TestCase
20+
{
21+
/**
22+
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
23+
*/
24+
public function testThrowExceptionIfStepOptionNotPositive()
25+
{
26+
$this->factory->create('range', null, array('min' => 0, 'max' => 100, 'step' => -5));
27+
}
28+
29+
/**
30+
* @expectedException \Symfony\Component\OptionsResolver\Exception\MissingOptionsException
31+
*/
32+
public function testThrowExceptionIfMaxOptionsMissing()
33+
{
34+
$this->factory->create('range', null, array('min' => 0));
35+
}
36+
37+
/**
38+
* @expectedException \Symfony\Component\OptionsResolver\Exception\MissingOptionsException
39+
*/
40+
public function testThrowExceptionIfMinOptionsMissing()
41+
{
42+
$this->factory->create('range', null, array('max' => 0));
43+
}
44+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy