8000 add allow_html5 option to date and time FormType to disable HTML5 dat… · symfony/symfony@392d6c7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 392d6c7

Browse files
csanquerCharles Sanquer
authored and
Charles Sanquer
committed
add allow_html5 option to date and time FormType to disable HTML5 date input when widget is set to single_text
remove unnecessary test when creating datetime format data transformer
1 parent b3b41d5 commit 392d6c7

File tree

8 files changed

+52
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
CHANGELOG
22
=========
33

4+
2.6.0
5+
-----
6+
* added allow_html5 option to Date, Time and DateTimeFormType to disable HTML5 input date when widget option is single_text
7+
48
2.5.0
59
------
610

Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
117117
'empty_value',
118118
'required',
119119
'translation_domain',
120+
'allow_html5',
120121
)));
121122

122123
$timeOptions = array_intersect_key($options, array_flip(array(
@@ -128,6 +129,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
128129
'empty_value',
129130
'required',
130131
'translation_domain',
132+
'allow_html5',
131133
)));
132134

133135
if (null !== $options['date_widget']) {
@@ -180,10 +182,11 @@ public function buildView(FormView $view, FormInterface $form, array $options)
180182
{
181183
$view->vars['widget'] = $options['widget'];
182184

183-
// Change the input to a HTML5 date input if
185+
// Change the input to a HTML5 datetime input if
184186
// * the widget is set to "single_text"
185187
// * the format matches the one expected by HTML5
186-
if ('single_text' === $options['widget'] && self::HTML5_FORMAT === $options['format']) {
188+
// * the allow_html5 is set to true
189+
if ($options['allow_html5'] && 'single_text' === $options['widget'] && self::HTML5_FORMAT === $options['format']) {
187190
$view->vars['type'] = 'datetime';
188191
}
189192
}
@@ -218,6 +221,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
218221
'time_widget' => $timeWidget,
219222
'with_minutes' => true,
220223
'with_seconds' => false,
224+
'allow_html5' => true,
221225
// Don't modify \DateTime classes by reference, we treat
222226
// them like immutable value objects
223227
'by_reference' => false,
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ public function finishView(FormView $view, FormInterface $form, array $options)
130130
// Change the input to a HTML5 date input if
131131
// * the widget is set to "single_text"
132132
// * the format matches the one expected by HTML5
133-
if ('single_text' === $options['widget'] && self::HTML5_FORMAT === $options['format']) {
133+
// * the allow_html5 is set to true
134+
if ($options['allow_html5'] && 'single_text' === $options['widget'] && self::HTML5_FORMAT === $options['format']) {
134135
$view->vars['type'] = 'date';
135136
}
136137

@@ -199,6 +200,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
199200
'model_timezone' => null,
200201
'view_timezone' => null,
201202
'empty_value' => $emptyValue,
203+
'allow_html5' => true,
202204
// Don't modify \DateTime classes by reference, we treat
203205
// them like immutable value objects
204206
'by_reference' => false,
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,10 @@ public function buildView(FormView $view, FormInterface $form, array $options)
138138
'with_seconds' => $options['with_seconds'],
139139
));
140140

141-
if ('single_text' === $options['widget']) {
141+
// Change the input to a HTML5 time input if
142+
// * the widget is set to "single_text"
143+
// * the allow_html5 is set to true
144+
if ($options['allow_html5'] && 'single_text' === $options['widget']) {
142145
$view->vars['type'] = 'time';
143146

144147
// we need to force the browser to display the seconds by
@@ -192,6 +195,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
192195
'model_timezone' => null,
193196
'view_timezone' => null,
194197
'empty_value' => $emptyValue,
198+
'allow_html5' => true,
195199
// Don't modify \DateTime classes by reference, we treat
196200
// them like immutable value objects
197201
'by_reference' => false,
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ protected function tearDown()
3232
$this->dateTimeWithoutSeconds = null;
3333
}
3434

35-
public static function assertEquals($expected, $actual, $message = '', $delta = 0, $maxDepth = 10, $canonicalize = FALSE, $ignoreCase = FALSE)
35+
public static function assertEquals($expected, $actual, $message = '', $delta = 0, $maxDepth = 10, $canonicalize = false, $ignoreCase = false)
3636
{
3737
if ($expected instanceof \DateTime && $actual instanceof \DateTime) {
3838
$expected = $expected->format('c');
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,17 @@ public function testPassHtml5TypeIfSingleTextAndHtml5Format()
405405
$this->assertSame('datetime', $view->vars['type']);
406406
}
407407

408+
public function testDontPassHtml5TypeIfHtml5NotAllowed()
409+
{
410+
$form = $this->factory->create('datetime', null, array(
411+
'widget' => 'single_text',
412+
'allow_html5' => false,
413+
));
414+
415+
$view = $form->createView();
416+
$this->assertFalse(isset($view->vars['type']));
417+
}
418+
408419
public function testDontPassHtml5TypeIfNotHtml5Format()
409420
{
410421
$form = $this->factory->create('datetime', null, array(
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,17 @@ public function testPassHtml5TypeIfSingleTextAndHtml5Format()
705705
$this->assertSame('date', $view->vars['type']);
706706
}
707707

708+
public function testDontPassHtml5TypeIfHtml5NotAllowed()
709+
{
710+
$form = $this->factory->create('date', null, array(
711+
'widget' => 'single_text',
712+
'allow_html5' => false,
713+
));
714+
715+
$view = $form->createView();
716+
$this->assertFalse(isset($view->vars['type']));
717+
}
718+
708719
public function testDontPassHtml5TypeIfNotHtml5Format()
709720
{
710721
$form = $this->factory->create('date', null, array(
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,17 @@ public function testSingleTextWidgetWithSecondsShouldNotOverrideStepAttribute()
519519
$this->assertEquals(30, $view->vars['attr']['step']);
520520
}
521521

522+
public function testDontPassHtml5TypeIfHtml5NotAllowed()
523+
{
524+
$form = $this->factory->create('time', null, array(
525+
'widget' => 'single_text',
526+
'allow_html5' => false,
527+
));
528+
529+
$view = $form->createView();
530+
$this->assertFalse(isset($view->vars['type']));
531+
}
532+
522533
public function testPassDefaultEmptyValueToViewIfNotRequired()
523534
{
524535
$form = $this->factory->create('time', null, array(