[Form] Renamed the value "text" of the "widget" option of the "date" type to "single-text"

This commit is contained in:
Bernhard Schussek 2011-05-13 19:17:28 +02:00
parent 7570e04589
commit e0ff61949e
6 changed files with 107 additions and 46 deletions

View File

@ -139,6 +139,10 @@ beta1 to beta2
* Serializer: The `$properties` argument has been dropped from all interfaces. * Serializer: The `$properties` argument has been dropped from all interfaces.
* Form: Renamed option value "text" of "widget" option of the "date" type was
renamed to "single-text". "text" indicates to use separate text boxes now
(like for the "time" type).
PR12 to beta1 PR12 to beta1
------------- -------------

View File

@ -1,4 +1,4 @@
<?php if ($widget == 'text'): ?> <?php if ($widget == 'single-text'): ?>
<input type="text" <input type="text"
<?php echo $view['form']->attributes() ?> <?php echo $view['form']->attributes() ?>
name="<?php echo $view->escape($name) ?>" name="<?php echo $view->escape($name) ?>"

View File

@ -151,7 +151,7 @@
{% block date_widget %} {% block date_widget %}
{% spaceless %} {% spaceless %}
{% if widget == 'text' %} {% if widget == 'single-text' %}
{{ block('text_widget') }} {{ block('text_widget') }}
{% else %} {% else %}
<div {{ block('attributes') }}> <div {{ block('attributes') }}>

View File

@ -35,29 +35,34 @@ class DateType extends AbstractType
\DateTimeZone::UTC \DateTimeZone::UTC
); );
if ($options['widget'] === 'text') { if ($options['widget'] === 'single-text') {
$builder->appendClientTransformer(new DateTimeToLocalizedStringTransformer($options['data_timezone'], $options['user_timezone'], $options['format'], \IntlDateFormatter::NONE)); $builder->appendClientTransformer(new DateTimeToLocalizedStringTransformer($options['data_timezone'], $options['user_timezone'], $options['format'], \IntlDateFormatter::NONE));
} else { } else {
// Only pass a subset of the options to children $yearOptions = $monthOptions = $dayOptions = array();
$yearOptions = array( $widget = $options['widget'];
'choice_list' => new PaddedChoiceList(
array_combine($options['years'], $options['years']), 4, '0', STR_PAD_LEFT
),
);
$monthOptions = array(
'choice_list' => new MonthChoiceList(
$formatter, $options['months']
),
);
$dayOptions = array(
'choice_list' => new PaddedChoiceList(
array_combine($options['days'], $options['days']), 2, '0', STR_PAD_LEFT
),
);
$builder->add('year', 'choice', $yearOptions) if ($widget === 'choice') {
->add('month', 'choice', $monthOptions) // Only pass a subset of the options to children
->add('day', 'choice', $dayOptions) $yearOptions = array(
'choice_list' => new PaddedChoiceList(
array_combine($options['years'], $options['years']), 4, '0', STR_PAD_LEFT
),
);
$monthOptions = array(
'choice_list' => new MonthChoiceList(
$formatter, $options['months']
),
);
$dayOptions = array(
'choice_list' => new PaddedChoiceList(
array_combine($options['days'], $options['days']), 2, '0', STR_PAD_LEFT
),
);
}
$builder->add('year', $widget, $yearOptions)
->add('month', $widget, $monthOptions)
->add('day', $widget, $dayOptions)
->appendClientTransformer(new DateTimeToArrayTransformer($options['data_timezone'], $options['user_timezone'], array('year', 'month', 'day'))); ->appendClientTransformer(new DateTimeToArrayTransformer($options['data_timezone'], $options['user_timezone'], array('year', 'month', 'day')));
} }
@ -132,6 +137,7 @@ class DateType extends AbstractType
'array', 'array',
), ),
'widget' => array( 'widget' => array(
'single-text',
'text', 'text',
'choice', 'choice',
), ),
@ -146,7 +152,7 @@ class DateType extends AbstractType
public function getParent(array $options) public function getParent(array $options)
{ {
return $options['widget'] === 'text' ? 'field' : 'form'; return $options['widget'] === 'single-text' ? 'field' : 'form';
} }
public function getName() public function getName()

View File

@ -597,6 +597,35 @@ abstract class AbstractLayoutTest extends \PHPUnit_Framework_TestCase
'widget' => 'text', 'widget' => 'text',
)); ));
$this->assertWidgetMatchesXpath($form->createView(), array(),
'/div
[
./input
[@id="na&me_month"]
[@type="text"]
[@value="2"]
/following-sibling::input
[@id="na&me_day"]
[@type="text"]
[@value="3"]
/following-sibling::input
[@id="na&me_year"]
[@type="text"]
[@value="2011"]
]
[count(./input)=3]
'
);
}
public function testDateSingleText()
{
$form = $this->factory->createNamed('date', 'na&me', '2011-02-03', array(
'property_path' => 'name',
'input' => 'string',
'widget' => 'single-text',
));
$this->assertWidgetMatchesXpath($form->createView(), array(), $this->assertWidgetMatchesXpath($form->createView(), array(),
'/input '/input
[@type="text"] [@type="text"]

View File

@ -45,12 +45,12 @@ class DateTypeTest extends LocalizedTestCase
)); ));
} }
public function testSubmitFromInputDateTime() public function testSubmitFromSingleTextDateTime()
{ {
$form = $this->factory->create('date', null, array( $form = $this->factory->create('date', null, array(
'data_timezone' => 'UTC', 'data_timezone' => 'UTC',
'user_timezone' => 'UTC', 'user_timezone' => 'UTC',
'widget' => 'text', 'widget' => 'single-text',
'input' => 'datetime', 'input' => 'datetime',
)); ));
@ -60,12 +60,12 @@ class DateTypeTest extends LocalizedTestCase
$this->assertEquals('02.06.2010', $form->getClientData()); $this->assertEquals('02.06.2010', $form->getClientData());
} }
public function testSubmitFromInputString() public function testSubmitFromSingleTextString()
{ {
$form = $this->factory->create('date', null, array( $form = $this->factory->create('date', null, array(
'data_timezone' => 'UTC', 'data_timezone' => 'UTC',
'user_timezone' => 'UTC', 'user_timezone' => 'UTC',
'widget' => 'text', 'widget' => 'single-text',
'input' => 'string', 'input' => 'string',
)); ));
@ -75,12 +75,12 @@ class DateTypeTest extends LocalizedTestCase
$this->assertEquals('02.06.2010', $form->getClientData()); $this->assertEquals('02.06.2010', $form->getClientData());
} }
public function testSubmitFromInputTimestamp() public function testSubmitFromSingleTextTimestamp()
{ {
$form = $this->factory->create('date', null, array( $form = $this->factory->create('date', null, array(
'data_timezone' => 'UTC', 'data_timezone' => 'UTC',
'user_timezone' => 'UTC', 'user_timezone' => 'UTC',
'widget' => 'text', 'widget' => 'single-text',
'input' => 'timestamp', 'input' => 'timestamp',
)); ));
@ -92,12 +92,12 @@ class DateTypeTest extends LocalizedTestCase
$this->assertEquals('02.06.2010', $form->getClientData()); $this->assertEquals('02.06.2010', $form->getClientData());
} }
public function testSubmitFromInputRaw() public function testSubmitFromSingleTextRaw()
{ {
$form = $this->factory->create('date', null, array( $form = $this->factory->create('date', null, array(
'data_timezone' => 'UTC', 'data_timezone' => 'UTC',
'user_timezone' => 'UTC', 'user_timezone' => 'UTC',
'widget' => 'text', 'widget' => 'single-text',
'input' => 'array', 'input' => 'array',
)); ));
@ -113,6 +113,28 @@ class DateTypeTest extends LocalizedTestCase
$this->assertEquals('02.06.2010', $form->getClientData()); $this->assertEquals('02.06.2010', $form->getClientData());
} }
public function testSubmitFromText()
{
$form = $this->factory->create('date', null, array(
'data_timezone' => 'UTC',
'user_timezone' => 'UTC',
'widget' => 'text',
));
$text = array(
'day' => '2',
'month' => '6',
'year' => '2010',
);
$form->bind($text);
$dateTime = new \DateTime('2010-06-02 UTC');
$this->assertDateTimeEquals($dateTime, $form->getData());
$this->assertEquals($text, $form->getClientData());
}
public function testSubmitFromChoice() public function testSubmitFromChoice()
{ {
$form = $this->factory->create('date', null, array( $form = $this->factory->create('date', null, array(
@ -163,7 +185,7 @@ class DateTypeTest extends LocalizedTestCase
'user_timezone' => 'Pacific/Tahiti', 'user_timezone' => 'Pacific/Tahiti',
// don't do this test with DateTime, because it leads to wrong results! // don't do this test with DateTime, because it leads to wrong results!
'input' => 'string', 'input' => 'string',
'widget' => 'text', 'widget' => 'single-text',
)); ));
$form->setData('2010-06-02'); $form->setData('2010-06-02');
@ -178,7 +200,7 @@ class DateTypeTest extends LocalizedTestCase
$form = $this->factory->create('date', null, array( $form = $this->factory->create('date', null, array(
'data_timezone' => 'UTC', 'data_timezone' => 'UTC',
'user_timezone' => 'UTC', 'user_timezone' => 'UTC',
'widget' => 'text', 'widget' => 'single-text',
'years' => array(2010, 2011), 'years' => array(2010, 2011),
)); ));
@ -194,7 +216,7 @@ class DateTypeTest extends LocalizedTestCase
$form = $this->factory->create('date', null, array( $form = $this->factory->create('date', null, array(
'data_timezone' => 'UTC', 'data_timezone' => 'UTC',
'user_timezone' => 'UTC', 'user_timezone' => 'UTC',
'widget' => 'text', 'widget' => 'single-text',
'years' => array(2010, 2011), 'years' => array(2010, 2011),
)); ));
@ -230,7 +252,7 @@ class DateTypeTest extends LocalizedTestCase
$form = $this->factory->create('date', null, array( $form = $this->factory->create('date', null, array(
'data_timezone' => 'UTC', 'data_timezone' => 'UTC',
'user_timezone' => 'UTC', 'user_timezone' => 'UTC',
'widget' => 'text', 'widget' => 'single-text',
'years' => array(2010, 2012), 'years' => array(2010, 2012),
)); ));
@ -246,7 +268,7 @@ class DateTypeTest extends LocalizedTestCase
$form = $this->factory->create('date', null, array( $form = $this->factory->create('date', null, array(
'data_timezone' => 'UTC', 'data_timezone' => 'UTC',
'user_timezone' => 'UTC', 'user_timezone' => 'UTC',
'widget' => 'text', 'widget' => 'single-text',
'months' => array(6, 7), 'months' => array(6, 7),
)); ));
@ -262,7 +284,7 @@ class DateTypeTest extends LocalizedTestCase
$form = $this->factory->create('date', null, array( $form = $this->factory->create('date', null, array(
'data_timezone' => 'UTC', 'data_timezone' => 'UTC',
'user_timezone' => 'UTC', 'user_timezone' => 'UTC',
'widget' => 'text', 'widget' => 'single-text',
'months' => array(6, 7), 'months' => array(6, 7),
)); ));
@ -298,7 +320,7 @@ class DateTypeTest extends LocalizedTestCase
$form = $this->factory->create('date', null, array( $form = $this->factory->create('date', null, array(
'data_timezone' => 'UTC', 'data_timezone' => 'UTC',
'user_timezone' => 'UTC', 'user_timezone' => 'UTC',
'widget' => 'text', 'widget' => 'single-text',
'months' => array(6, 8), 'months' => array(6, 8),
)); ));
@ -314,7 +336,7 @@ class DateTypeTest extends LocalizedTestCase
$form = $this->factory->create('date', null, array( $form = $this->factory->create('date', null, array(
'data_timezone' => 'UTC', 'data_timezone' => 'UTC',
'user_timezone' => 'UTC', 'user_timezone' => 'UTC',
'widget' => 'text', 'widget' => 'single-text',
'days' => array(6, 7), 'days' => array(6, 7),
)); ));
@ -330,7 +352,7 @@ class DateTypeTest extends LocalizedTestCase
$form = $this->factory->create('date', null, array( $form = $this->factory->create('date', null, array(
'data_timezone' => 'UTC', 'data_timezone' => 'UTC',
'user_timezone' => 'UTC', 'user_timezone' => 'UTC',
'widget' => 'text', 'widget' => 'single-text',
'days' => array(6, 7), 'days' => array(6, 7),
)); ));
@ -368,7 +390,7 @@ class DateTypeTest extends LocalizedTestCase
$form = $this->factory->create('date', null, array( $form = $this->factory->create('date', null, array(
'data_timezone' => 'UTC', 'data_timezone' => 'UTC',
'user_timezone' => 'UTC', 'user_timezone' => 'UTC',
'widget' => 'text', 'widget' => 'single-text',
'days' => array(6, 8), 'days' => array(6, 8),
)); ));
@ -377,14 +399,14 @@ class DateTypeTest extends LocalizedTestCase
$this->assertFalse($form->isDayWithinRange()); $this->assertFalse($form->isDayWithinRange());
} }
public function testIsPartiallyFilledReturnsFalseIfInput() public function testIsPartiallyFilledReturnsFalseIfSingleText()
{ {
$this->markTestIncomplete('Needs to be reimplemented using validators'); $this->markTestIncomplete('Needs to be reimplemented using validators');
$form = $this->factory->create('date', null, array( $form = $this->factory->create('date', null, array(
'data_timezone' => 'UTC', 'data_timezone' => 'UTC',
'user_timezone' => 'UTC', 'user_timezone' => 'UTC',
'widget' => 'text', 'widget' => 'single-text',
)); ));
$form->bind('7.6.2010'); $form->bind('7.6.2010');
@ -460,7 +482,7 @@ class DateTypeTest extends LocalizedTestCase
public function testDontPassDatePatternIfText() public function testDontPassDatePatternIfText()
{ {
$form = $this->factory->create('date', null, array( $form = $this->factory->create('date', null, array(
'widget' => 'text', 'widget' => 'single-text',
)); ));
$view = $form->createView(); $view = $form->createView();
@ -470,10 +492,10 @@ class DateTypeTest extends LocalizedTestCase
public function testPassWidgetToView() public function testPassWidgetToView()
{ {
$form = $this->factory->create('date', null, array( $form = $this->factory->create('date', null, array(
'widget' => 'text', 'widget' => 'single-text',
)); ));
$view = $form->createView(); $view = $form->createView();
$this->assertSame('text', $view->get('widget')); $this->assertSame('single-text', $view->get('widget'));
} }
} }