8000 feature #8291 [Form] Add allow_html5 option to date and time FormType… · symfony/symfony@0da03cf · GitHub
[go: up one dir, main page]

Skip to content

Commit 0da03cf

Browse files
committed
feature #8291 [Form] Add allow_html5 option to date and time FormType to disable HTML5 input type (csanquer)
This PR was merged into the 2.6-dev branch. Discussion ---------- [Form] Add allow_html5 option to date and time FormType to disable HTML5 input type [Form] added allow_html5 option to date and time FormType to disable HTML5 input type when widget is set to single_text | Q | A | --------------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #6927 #7123 | License | MIT | Doc PR | With this little patch we can have a single text widget without HTML5 date input type which is required when using some javascript date or time picker . Commits ------- 392d6c7 add allow_html5 option to date and time FormType to disable HTML5 date input when widget is set to single_text
2 parents 8ac1225 + 392d6c7 commit 0da03cf

File tree

7 files changed

+51
-4
lines changed

7 files changed

+51
-4
lines changed

src/Symfony/Component/Form/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
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

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

Lines changed: 6 additions & 2 deletions
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,

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ public function finishView(FormView $view, FormInterface $form, array $options)
136136
// Change the input to a HTML5 date input if
137137
// * the widget is set to "single_text"
138138
// * the format matches the one expected by HTML5
139-
if ('single_text' === $options['widget'] && self::HTML5_FORMAT === $options['format']) {
139+
// * the allow_html5 is set to true
140+
if ($options['allow_html5'] && 'single_text' === $options['widget'] && self::HTML5_FORMAT === $options['format']) {
140141
$view->vars['type'] = 'date';
141142
}
142143

@@ -205,6 +206,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
205206
'model_timezone' => null,
206207
'view_timezone' => null,
207208
'empty_value' => $emptyValue,
209+
'allow_html5' => true,
208210
// Don't modify \DateTime classes by reference, we treat
209211
// them like immutable value objects
210212
'by_reference' => false,

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

Lines changed: 5 additions & 1 deletion
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,

src/Symfony/Component/Form/Tests/Extension/Core/Type/DateTimeTypeTest.php

Lines changed: 11 additions & 0 deletions
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(

src/Symfony/Component/Form/Tests/Extension/Core/Type/DateTypeTest.php

Lines changed: 11 additions & 0 deletions
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(

src/Symfony/Component/Form/Tests/Extension/Core/Type/TimeTypeTest.php

Lines changed: 11 additions & 0 deletions
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(

0 commit comments

Comments
 (0)
0