Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Form] add option to render NumberType as type="number" #30267

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

Merged
merged 1 commit into from
Feb 21, 2019
Merged
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
add option to render NumberType as type="number"
  • Loading branch information
xabbuh committed Feb 21, 2019
commit 42e8f5e3a240aa2d05e4a79a00df605a969f088b
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@
{%- endblock dateinterval_widget -%}

{%- block number_widget -%}
{# type="number" doesn't work with floats #}
{# type="number" doesn't work with floats in localized formats #}
{%- set type = type|default('text') -%}
{{ block('form_widget_simple') }}
{%- endblock number_widget -%}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2089,6 +2089,22 @@ public function testNumber()
);
}

public function testRenderNumberWithHtml5NumberType()
{
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\NumberType', 1234.56, [
javiereguiluz marked this conversation as resolved.
Show resolved Hide resolved
'html5' => true,
]);

$this->assertWidgetMatchesXpath($form->createView(), ['attr' => ['class' => 'my&class']],
'/input
[@type="number"]
[@name="name"]
[@class="my&class form-control"]
[@value="1234.56"]
'
);
}

public function testPassword()
{
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\PasswordType', 'foo&bar');
Expand Down
1 change: 1 addition & 0 deletions 1 src/Symfony/Component/Form/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
4.3.0
-----

* added `html5` option to `NumberType` that allows to render `type="number"` input fields
* deprecated using the `date_format`, `date_widget`, and `time_widget` options of the `DateTimeType` when the `widget`
option is set to `single_text`
* added `block_prefix` option to `BaseType`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ class NumberToLocalizedStringTransformer implements DataTransformerInterface
protected $roundingMode;

private $scale;
private $locale;

public function __construct(int $scale = null, ?bool $grouping = false, ?int $roundingMode = self::ROUND_HALF_UP)
public function __construct(int $scale = null, ?bool $grouping = false, ?int $roundingMode = self::ROUND_HALF_UP, string $locale = null)
{
if (null === $grouping) {
$grouping = false;
Expand All @@ -91,6 +92,7 @@ public function __construct(int $scale = null, ?bool $grouping = false, ?int $ro
$this->scale = $scale;
$this->grouping = $grouping;
$this->roundingMode = $roundingMode;
$this->locale = $locale;
}

/**
Expand Down Expand Up @@ -214,7 +216,7 @@ public function reverseTransform($value)
*/
protected function getNumberFormatter()
{
$formatter = new \NumberFormatter(\Locale::getDefault(), \NumberFormatter::DECIMAL);
$formatter = new \NumberFormatter($this->locale ?? \Locale::getDefault(), \NumberFormatter::DECIMAL);

if (null !== $this->scale) {
$formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $this->scale);
Expand Down
27 changes: 26 additions & 1 deletion 27 src/Symfony/Component/Form/Extension/Core/Type/NumberType.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@
namespace Symfony\Component\Form\Extension\Core\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Exception\LogicException;
use Symfony\Component\Form\Extension\Core\DataTransformer\NumberToLocalizedStringTransformer;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

class NumberType extends AbstractType
Expand All @@ -26,10 +30,21 @@ public function buildForm(FormBuilderInterface $builder, array $options)
$builder->addViewTransformer(new NumberToLocalizedStringTransformer(
$options['scale'],
$options['grouping'],
$options['rounding_mode']
$options['rounding_mode'],
$options['html5'] ? 'en' : null
fabpot marked this conversation as resolved.
Show resolved Hide resolved
));
}

/**
* {@inheritdoc}
*/
public function buildView(FormView $view, FormInterface $form, array $options)
{
if ($options['html5']) {
$view->vars['type'] = 'number';
}
}

/**
* {@inheritdoc}
*/
Expand All @@ -41,6 +56,7 @@ public function configureOptions(OptionsResolver $resolver)
'grouping' => false,
'rounding_mode' => NumberToLocalizedStringTransformer::ROUND_HALF_UP,
'compound' => false,
'html5' => false,
]);

$resolver->setAllowedValues('rounding_mode', [
Expand All @@ -54,6 +70,15 @@ public function configureOptions(OptionsResolver $resolver)
]);

$resolver->setAllowedTypes('scale', ['null', 'int']);
$resolver->setAllowedTypes('html5', 'bool');

$resolver->setNormalizer('grouping', function (Options $options, $value) {
if (true === $value && $options['html5']) {
throw new LogicException('Cannot use the "grouping" option when the "html5" option is enabled.');
}

return $value;
});
}

/**
Expand Down
17 changes: 17 additions & 0 deletions 17 src/Symfony/Component/Form/Tests/AbstractLayoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1871,6 +1871,23 @@ public function testNumber()
);
}

public function testRenderNumberWithHtml5NumberType()
{
$this->requiresFeatureSet(403);

$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\NumberType', 1234.56, [
'html5' => true,
]);

$this->assertWidgetMatchesXpath($form->createView(), [],
'/input
[@type="number"]
[@name="name"]
[@value="1234.56"]
'
);
}

public function testPassword()
{
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\PasswordType', 'foo&bar');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,28 @@ public function testSubmitNullUsesDefaultEmptyData($emptyData = '10', $expectedD
$this->assertSame($expectedData, $form->getNormData());
$this->assertSame($expectedData, $form->getData());
}

public function testIgnoresDefaultLocaleToRenderHtml5NumberWidgets()
{
$form = $this->factory->create(static::TESTED_TYPE, null, [
'scale' => 2,
'rounding_mode' => \NumberFormatter::ROUND_UP,
'html5' => true,
]);
$form->setData(12345.54321);

$this->assertSame('12345.55', $form->createView()->vars['value']);
$this->assertSame('12345.55', $form->getViewData());
}

/**
* @expectedException \Symfony\Component\Form\Exception\LogicException
*/
public function testGroupingNotAllowedWithHtml5Widget()
{
$this->factory->create(static::TESTED_TYPE, null, [
'grouping' => true,
'html5' => true,
]);
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.