Skip to content

Commit

Permalink
feature #8291 [Form] Add allow_html5 option to date and time FormType…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
fabpot committed Sep 23, 2014
2 parents 8ac1225 + 392d6c7 commit 0da03cf
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
@@ -1,6 +1,10 @@
CHANGELOG
=========

2.6.0
-----
* added allow_html5 option to Date, Time and DateTimeFormType to disable HTML5 input date when widget option is single_text

2.5.0
------

Expand Down
Expand Up @@ -117,6 +117,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'empty_value',
'required',
'translation_domain',
'allow_html5',
)));

$timeOptions = array_intersect_key($options, array_flip(array(
Expand All @@ -128,6 +129,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'empty_value',
'required',
'translation_domain',
'allow_html5',
)));

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

// Change the input to a HTML5 date input if
// Change the input to a HTML5 datetime input if
// * the widget is set to "single_text"
// * the format matches the one expected by HTML5
if ('single_text' === $options['widget'] && self::HTML5_FORMAT === $options['format']) {
// * the allow_html5 is set to true
if ($options['allow_html5'] && 'single_text' === $options['widget'] && self::HTML5_FORMAT === $options['format']) {
$view->vars['type'] = 'datetime';
}
}
Expand Down Expand Up @@ -218,6 +221,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
'time_widget' => $timeWidget,
'with_minutes' => true,
'with_seconds' => false,
'allow_html5' => true,
// Don't modify \DateTime classes by reference, we treat
// them like immutable value objects
'by_reference' => false,
Expand Down
4 changes: 3 additions & 1 deletion src/Symfony/Component/Form/Extension/Core/Type/DateType.php
Expand Up @@ -136,7 +136,8 @@ public function finishView(FormView $view, FormInterface $form, array $options)
// Change the input to a HTML5 date input if
// * the widget is set to "single_text"
// * the format matches the one expected by HTML5
if ('single_text' === $options['widget'] && self::HTML5_FORMAT === $options['format']) {
// * the allow_html5 is set to true
if ($options['allow_html5'] && 'single_text' === $options['widget'] && self::HTML5_FORMAT === $options['format']) {
$view->vars['type'] = 'date';
}

Expand Down Expand Up @@ -205,6 +206,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
'model_timezone' => null,
'view_timezone' => null,
'empty_value' => $emptyValue,
'allow_html5' => true,
// Don't modify \DateTime classes by reference, we treat
// them like immutable value objects
'by_reference' => false,
Expand Down
6 changes: 5 additions & 1 deletion src/Symfony/Component/Form/Extension/Core/Type/TimeType.php
Expand Up @@ -138,7 +138,10 @@ public function buildView(FormView $view, FormInterface $form, array $options)
'with_seconds' => $options['with_seconds'],
));

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

// we need to force the browser to display the seconds by
Expand Down Expand Up @@ -192,6 +195,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
'model_timezone' => null,
'view_timezone' => null,
'empty_value' => $emptyValue,
'allow_html5' => true,
// Don't modify \DateTime classes by reference, we treat
// them like immutable value objects
'by_reference' => false,
Expand Down
Expand Up @@ -405,6 +405,17 @@ public function testPassHtml5TypeIfSingleTextAndHtml5Format()
$this->assertSame('datetime', $view->vars['type']);
}

public function testDontPassHtml5TypeIfHtml5NotAllowed()
{
$form = $this->factory->create('datetime', null, array(
'widget' => 'single_text',
'allow_html5' => false,
));

$view = $form->createView();
$this->assertFalse(isset($view->vars['type']));
}

public function testDontPassHtml5TypeIfNotHtml5Format()
{
$form = $this->factory->create('datetime', null, array(
Expand Down
Expand Up @@ -705,6 +705,17 @@ public function testPassHtml5TypeIfSingleTextAndHtml5Format()
$this->assertSame('date', $view->vars['type']);
}

public function testDontPassHtml5TypeIfHtml5NotAllowed()
{
$form = $this->factory->create('date', null, array(
'widget' => 'single_text',
'allow_html5' => false,
));

$view = $form->createView();
$this->assertFalse(isset($view->vars['type']));
}

public function testDontPassHtml5TypeIfNotHtml5Format()
{
$form = $this->factory->create('date', null, array(
Expand Down
Expand Up @@ -519,6 +519,17 @@ public function testSingleTextWidgetWithSecondsShouldNotOverrideStepAttribute()
$this->assertEquals(30, $view->vars['attr']['step']);
}

public function testDontPassHtml5TypeIfHtml5NotAllowed()
{
$form = $this->factory->create('time', null, array(
'widget' => 'single_text',
'allow_html5' => false,
));

$view = $form->createView();
$this->assertFalse(isset($view->vars['type']));
}

public function testPassDefaultEmptyValueToViewIfNotRequired()
{
$form = $this->factory->create('time', null, array(
Expand Down

0 comments on commit 0da03cf

Please sign in to comment.