8000 [Bridge\Twig] Add 'form-control-range' for range input type · grasmash/symfony@e73db2b · GitHub
[go: up one dir, main page]

Skip to content

Commit e73db2b

Browse files
Oviglonicolas-grekas
authored andcommitted
[Bridge\Twig] Add 'form-control-range' for range input type
1 parent abf16d0 commit e73db2b

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_4_layout.html.twig

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,22 +132,28 @@
132132
{% endblock %}
133133

134134
{% block form_widget_simple -%}
135-
{% if type is not defined or type != 'hidden' %}
136-
{%- set attr = attr|merge({class: (attr.class|default('') ~ (type|default('') == 'file' ? ' custom-file-input' : ' form-control'))|trim}) -%}
137-
{% endif %}
135+
{%- if type is not defined or type != 'hidden' -%}
136+
{%- set className = ' form-control' -%}
137+
{%- if type|default('') == 'file' -%}
138+
{%- set className = ' custom-file-input' -%}
139+
{%- elseif type|default('') == 'range' -%}
140+
{%- set className = ' form-control-range' -%}
141+
{%- endif -%}
142+
{%- set attr = attr|merge({class: (attr.class|default('') ~ className)|trim}) -%}
143+
{%- endif -%}
138144
{%- if type is defined and (type == 'range' or type == 'color') %}
139145
{# Attribute "required" is not supported #}
140146
{%- set required = false -%}
141147
{% endif %}
142148
{{- parent() -}}
143149
{%- endblock form_widget_simple %}
144150

145-
{%- block widget_attributes -%}
146-
{%- if not valid %}
151+
{% block widget_attributes -%}
152+
{%- if not valid -%}
147153
{% set attr = attr|merge({class: (attr.class|default('') ~ ' is-invalid')|trim}) %}
148-
{% endif -%}
154+
{%- endif -%}
149155
{{ parent() }}
150-
{%- endblock widget_attributes -%}
156+
{%- endblock widget_attributes %}
151157

152158
{% block button_widget -%}
153159
{%- set attr = attr|merge({class: (attr.class|default('btn-secondary') ~ ' btn')|trim}) -%}

src/Symfony/Bridge/Twig/Tests/Extension/AbstractBootstrap4LayoutTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\Form\Extension\Core\Type\MoneyType;
2020
use Symfony\Component\Form\Extension\Core\Type\PercentType;
2121
use Symfony\Component\Form\Extension\Core\Type\RadioType;
22+
use Symfony\Component\Form\Extension\Core\Type\RangeType;
2223
use Symfony\Component\Form\Extension\Core\Type\TextType;
2324
use Symfony\Component\Form\FormError;
2425

@@ -1194,6 +1195,41 @@ public function testPercentCustomSymbol()
11941195
[contains(.., "‱")]
11951196
]
11961197
]
1198+
'
1199+
);
1200+
}
1201+
1202+
public function testRange()
1203+
{
1204+
$form = $this->factory->createNamed('name', RangeType::class, 42, ['attr' => ['min' => 5]]);
1205+
1206+
$this->assertWidgetMatchesXpath(
1207+
$form->createView(),
1208+
['attr' => ['class' => 'my&class']],
1209+
'/input
1210+
[@type="range"]
1211+
[@name="name"]
1212+
[@value="42"]
1213+
[@min="5"]
1214+
[@class="my&class form-control-range"]
1215+
'
1216+
);
1217+
}
1218+
1219+
public function testRangeWithMinMaxValues()
1220+
{
1221+
$form = $this->factory->createNamed('name', RangeType::class, 42, ['attr' => ['min' => 5, 'max' => 57]]);
1222+
1223+
$this->assertWidgetMatchesXpath(
1224+
$form->createView(),
1225+
['attr' => ['class' => 'my&class']],
1226+
'/input
1227+
[@type="range"]
1228+
[@name="name"]
1229+
[@value="42"]
1230+
[@min="5"]
1231+
[@max="57"]
1232+
[@class="my&class form-control-range"]
11971233
'
11981234
);
11991235
}

0 commit comments

Comments
 (0)
0