Skip to content

[Form] Add "range" type #11979 #14289

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@
{{ block('form_widget_simple') }}
{%- endblock integer_widget -%}

{%- block range_widget -%}
{% set type = type|default('range') %}
{{- block('form_widget_simple') -}}
{%- endblock range_widget -%}

{%- block money_widget -%}
{{ money_pattern|replace({ '{{ widget }}': block('form_widget_simple') })|raw }}
{%- endblock money_widget -%}
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/form.xml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@
<service id="form.type.currency" class="Symfony\Component\Form\Extension\Core\Type\CurrencyType">
<tag name="form.type" alias="currency" />
</service>
<service id="form.type.range" class="Symfony\Component\Form\Extension\Core\Type\RangeType">
<tag name="form.type" alias="range" />
</service>

<!-- FormTypeHttpFoundationExtension -->
<service id="form.type_extension.form.http_foundation" class="Symfony\Component\Form\Extension\HttpFoundation\Type\FormTypeHttpFoundationExtension">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ protected function loadTypes()
new Type\EmailType(),
new Type\HiddenType(),
new Type\IntegerType(),
new Type\RangeType(),
new Type\LanguageType(),
new Type\LocaleType(),
new Type\MoneyType(),
Expand Down
73 changes: 73 additions & 0 deletions src/Symfony/Component/Form/Extension/Core/Type/RangeType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

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

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* A range form element.
*
* @author Carlos Revillo <crevillo@gmail.com>
* @author Pawel Krynicki <pawel.krynicki@hotmail.com>
*/
class RangeType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars['attr']['min'] = $options['min'];
$view->vars['attr']['max'] = $options['max'];

if (isset($options['step'])) {
$view->vars['attr']['step'] = $options['step'];
}
}

/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setRequired(array('min', 'max'));

$resolver->setDefined('step');

$resolver->setAllowedTypes('min', 'numeric');
$resolver->setAllowedTypes('max', 'numeric');
$resolver->setAllowedTypes('step', 'numeric');

$resolver->setAllowedValues('step', function ($value) {
return $value > 0;
});
}

/**
* {@inheritdoc}
*/
public function getParent()
{
return 'number';
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'range';
}
}
21 changes: 21 additions & 0 deletions src/Symfony/Component/Form/Tests/AbstractBootstrap3LayoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1388,6 +1388,27 @@ public function testInteger()
);
}

public function testRange()
{
$form = $this->factory->createNamed('name', 'range', 123.45, array(
'min' => 1.25,
'max' => 234.56,
'step' => 0.25,
));

$this->assertWidgetMatchesXpath($form->createView(), array('attr' => array('class' => 'my&class')),
'/input
[@type="range"]
[@name="name"]
[@min="1.25"]
[@max="234.56"]
[@step="0.25"]
[@class="my&class form-control"]
[@value="123.45"]
'
);
}

public function testLanguage()
{
$form = $this->factory->createNamed('name', 'language', 'de');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Form\Tests\Extension\Core\Type;

use Symfony\Component\Form\Test\TypeTestCase as TestCase;

/**
* @author Pawel Krynicki <pawel.krynicki@hotmail.com>
*/
class RangeTypeTest extends TestCase
{
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/
public function testThrowExceptionIfStepOptionNotPositive()
{
$this->factory->create('range', null, array('min' => 0, 'max' => 100, 'step' => -5));
}

/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\MissingOptionsException
*/
public function testThrowExceptionIfMaxOptionsMissing()
{
$this->factory->create('range', null, array('min' => 0));
}

/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\MissingOptionsException
*/
public function testThrowExceptionIfMinOptionsMissing()
{
$this->factory->create('range', null, array('max' => 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