Skip to content

Commit

Permalink
merged branch willdurand/form-date-types (PR #4204)
Browse files Browse the repository at this point in the history
Commits
-------

ceb5ce6 [Form] fixed tests
a1e3a59 [TwigBridge] Switched to composer
df36afb [Form] Added tests
6d5ad3b [Form] Added right HTML types to Datetime/Date/Time types if single_text is true

Discussion
----------

[Form] Added right HTML types to Datetime/Date/Time types if single_text is true

When you set the `widget` option to `single_text`, you get a HTML input tag which is fine, but you the type is `text`, and it's wrong. You don't have any other way to get the right `type` as this attribute is defined to the FormView instance itself (see FileType for instance).

This PR adds right HTML types like the FileType does.

Cheers,
William

---------------------------------------------------------------------------

by willdurand at 2012-05-09T16:04:16Z

@fabpot anything else to do there?

---------------------------------------------------------------------------

by fabpot at 2012-05-11T16:28:43Z

adding some unit tests?

---------------------------------------------------------------------------

by willdurand at 2012-05-11T16:35:40Z

fair point :)

---------------------------------------------------------------------------

by travisbot at 2012-05-12T16:34:43Z

This pull request [fails](http://travis-ci.org/symfony/symfony/builds/1314731) (merged 2631c8b7 into cb905c5).

---------------------------------------------------------------------------

by travisbot at 2012-05-12T17:14:12Z

This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1314902) (merged ceb5ce6 into e193452).

---------------------------------------------------------------------------

by willdurand at 2012-05-12T17:16:17Z

@fabpot ok, so I had to fix some other tests but there is a weird dependency between the tests in TwigBridge, and the Form component. I fixed the test suite's setup in the TwigBridge, and fixed some failing tests.
  • Loading branch information
fabpot committed May 14, 2012
2 parents 6fba6d7 + ceb5ce6 commit 46ffbd5
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 48 deletions.
2 changes: 2 additions & 0 deletions src/Symfony/Bridge/Twig/.gitignore
@@ -0,0 +1,2 @@
vendor/
composer.lock
16 changes: 4 additions & 12 deletions src/Symfony/Bridge/Twig/README.md
Expand Up @@ -7,17 +7,9 @@ Symfony2 components.
Resources
---------

You can run the unit tests with the following command:
If you want to run the unit tests, install dev dependencies before
running PHPUnit:

phpunit -c src/Symfony/Bridge/Twig/
php composer.phar install --dev

If you also want to run the unit tests that depend on other Symfony
Components, declare the following environment variables before running
PHPUnit:

export TWIG=../path/to/Twig
export SYMFONY_EVENT_DISPATCHER=../path/to/EventDispatcher
export SYMFONY_FORM=../path/to/Form
export SYMFONY_LOCALE=../path/to/Locale
export SYMFONY_TEMPLATING=../path/to/Templating
export SYMFONY_TRANSLATION=../path/to/Translation
phpunit
Expand Up @@ -11,12 +11,12 @@

namespace Symfony\Bridge\Twig\Tests\Extension;

use Symfony\Component\Form\FormView;
use Symfony\Bridge\Twig\Extension\FormExtension;
use Symfony\Bridge\Twig\Extension\TranslationExtension;
use Symfony\Component\Form\Tests\AbstractDivLayoutTest;
use Symfony\Bridge\Twig\Tests\Extension\Fixtures\StubTranslator;
use Symfony\Bridge\Twig\Tests\Extension\Fixtures\StubFilesystemLoader;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\Tests\AbstractDivLayoutTest;

class FormExtensionDivLayoutTest extends AbstractDivLayoutTest
{
Expand Down
30 changes: 3 additions & 27 deletions src/Symfony/Bridge/Twig/Tests/bootstrap.php
Expand Up @@ -9,30 +9,6 @@
* file that was distributed with this source code.
*/

spl_autoload_register(function ($class) {
foreach (array(
'SYMFONY_EVENT_DISPATCHER' => 'EventDispatcher',
'SYMFONY_FORM' => 'Form',
'SYMFONY_LOCALE' => 'Locale',
'SYMFONY_TEMPLATING' => 'Templating',
'SYMFONY_TRANSLATION' => 'Translation',
) as $env => $name) {
if (isset($_SERVER[$env]) && 0 === strpos(ltrim($class, '/'), 'Symfony\Component\\'.$name)) {
if (file_exists($file = $_SERVER[$env].'/'.substr(str_replace('\\', '/', $class), strlen('Symfony\Component\\'.$name)).'.php')) {
require_once $file;
}
}
}

if (isset($_SERVER['TWIG']) && 0 === strpos(ltrim($class, '/'), 'Twig_')) {
if (file_exists($file = $_SERVER['TWIG'].'/lib/'.str_replace('_', '/', $class).'.php')) {
require_once $file;
}
}

if (0 === strpos(ltrim($class, '/'), 'Symfony\Bridge\Twig')) {
if (file_exists($file = __DIR__.'/../'.substr(str_replace('\\', '/', $class), strlen('Symfony\Bridge\Twig')).'.php')) {
require_once $file;
}
}
});
if (file_exists($loader = __DIR__.'/../vendor/autoload.php')) {
require_once $loader;
}
7 changes: 7 additions & 0 deletions src/Symfony/Bridge/Twig/composer.json
Expand Up @@ -19,6 +19,13 @@
"php": ">=5.3.3",
"twig/twig": ">=1.8,<2.0-dev"
},
"require-dev": {
"symfony/form": "2.1.*",
"symfony/routing": "2.1.*",
"symfony/templating": "2.1.*",
"symfony/translation": "2.1.*",
"symfony/yaml": "2.1.*"
},
"suggest": {
"symfony/form": "self.version",
"symfony/routing": "self.version",
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Bridge/Twig/phpunit.xml.dist
Expand Up @@ -9,7 +9,7 @@
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="tests/bootstrap.php"
bootstrap="Tests/bootstrap.php"
>
<testsuites>
<testsuite name="Symfony Twig Bridge Test Suite">
Expand All @@ -23,6 +23,7 @@
<exclude>
<directory>./Resources</directory>
<directory>./Tests</directory>
<directory>./vendor</directory>
</exclude>
</whitelist>
</filter>
Expand Down
Expand Up @@ -124,6 +124,10 @@ public function buildForm(FormBuilder $builder, array $options)
public function buildView(FormView $view, FormInterface $form)
{
$view->set('widget', $form->getAttribute('widget'));

if ('single_text' === $form->getAttribute('widget')) {
$view->set('type', 'datetime');
}
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Form/Extension/Core/Type/DateType.php
Expand Up @@ -142,6 +142,10 @@ public function buildViewBottomUp(FormView $view, FormInterface $form)
{
$view->set('widget', $form->getAttribute('widget'));

if ('single_text' === $form->getAttribute('widget')) {
$view->set('type', 'date');
}

if ($view->hasChildren()) {
$pattern = $form->getAttribute('formatter')->getPattern();

Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Form/Extension/Core/Type/TimeType.php
Expand Up @@ -130,6 +130,10 @@ public function buildView(FormView $view, FormInterface $form)
->set('widget', $form->getAttribute('widget'))
->set('with_seconds', $form->getAttribute('with_seconds'))
;

if ('single_text' === $form->getAttribute('widget')) {
$view->set('type', 'time');
}
}

/**
Expand Down
12 changes: 6 additions & 6 deletions src/Symfony/Component/Form/Tests/AbstractLayoutTest.php
Expand Up @@ -947,12 +947,12 @@ public function testDateTimeSingleText()
'/div
[
./input
[@type="text"]
[@type="date"]
[@id="name_date"]
[@name="name[date]"]
[@value="Feb 3, 2011"]
/following-sibling::input
[@type="text"]
[@type="time"]
[@id="name_time"]
[@name="name[time]"]
[@value="04:05"]
Expand All @@ -970,7 +970,7 @@ public function testDateTimeWithWidgetSingleText()

$this->assertWidgetMatchesXpath($form->createView(), array(),
'/input
[@type="text"]
[@type="datetime"]
[@name="name"]
[@value="2011-02-03 04:05"]
'
Expand All @@ -988,7 +988,7 @@ public function testDateTimeWithWidgetSingleTextIgnoreDateAndTimeWidgets()

$this->assertWidgetMatchesXpath($form->createView(), array(),
'/input
[@type="text"]
[@type="datetime"]
[@name="name"]
[@value="2011-02-03 04:05"]
'
Expand Down Expand Up @@ -1111,7 +1111,7 @@ public function testDateSingleText()

$this->assertWidgetMatchesXpath($form->createView(), array(),
'/input
[@type="text"]
[@type="date"]
[@name="name"]
[@value="Feb 3, 2011"]
'
Expand Down Expand Up @@ -1586,7 +1586,7 @@ public function testTimeSingleText()

$this->assertWidgetMatchesXpath($form->createView(), array(),
'/input
[@type="text"]
[@type="time"]
[@name="name"]
[@value="04:05"]
'
Expand Down
Expand Up @@ -259,4 +259,14 @@ public function testInitializeWithDateTime()
// to null in the type
$this->factory->create('datetime', new \DateTime());
}

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

$view = $form->createView();
$this->assertEquals('datetime', $view->get('type'));
}
}
Expand Up @@ -536,4 +536,14 @@ public function testInitializeWithDateTime()
// to null in the type
$this->factory->create('date', new \DateTime());
}

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

$view = $form->createView();
$this->assertEquals('date', $view->get('type'));
}
}
Expand Up @@ -406,4 +406,14 @@ public function testInitializeWithDateTime()
// to null in the type
$this->factory->create('time', new \DateTime());
}

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

$view = $form->createView();
$this->assertEquals('time', $view->get('type'));
}
}

0 comments on commit 46ffbd5

Please sign in to comment.