Skip to content
Browse files

Throw exception when "date_widget" option is not equal to "time_widget"

  • Loading branch information...
1 parent 305c476 commit 3c2539fccbaaded0025c96bf56b7767440b2b28f @stloyd stloyd committed Jun 23, 2011
View
5 src/Symfony/Component/Form/Extension/Core/Type/DateTimeType.php
@@ -16,6 +16,7 @@
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\ReversedTransformer;
+use Symfony\Component\Form\Exception\FormException;
use Symfony\Component\Form\Extension\Core\DataTransformer\DataTransformerChain;
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToArrayTransformer;
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToStringTransformer;
@@ -37,6 +38,10 @@ public function buildForm(FormBuilder $builder, array $options)
$timeParts[] = 'second';
}
+ if ($options['date_widget'] !== $options['time_widget']) {
@coderbyheart
coderbyheart added a note Nov 11, 2011

Can you elaborate, why this is a requirement?

I created a type with a text date field (for a js datepicker) and a dropdown for the time which works without a problem if i disable this check.

@wodor
wodor added a note Dec 28, 2011

seems I've gone through the same path as @tacker,
This exception is the only thing which stops me from just inherit from DateTimeType in DatePickerType.
This way I could avoid copying code of buildForm method https://github.com/wodor/HudsonJQueryBundle/blob/datePickerType/Form/DatePickerType.php
(this is ugly, but working, date_widget value is changed after this 'if')
Everything necessary for displaying datepicker, next to drop-down hour and minute,
is setting reasonable default options and changing getName to return 'datepicker'. (and creating appropriate block)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ throw new FormException(sprintf('Options "date_widget" and "time_widget" need to be identical. Used: "date_widget" = "%s" and "time_widget" = "%s".', $options['date_widget'] ?: 'choice', $options['time_widget'] ?: 'choice'));
+ }
+
if ($options['widget'] === 'single_text') {
$builder->appendClientTransformer(new DateTimeToStringTransformer($options['data_timezone'], $options['user_timezone'], 'Y-m-d H:i:s'));
} else {
View
21 tests/Symfony/Tests/Component/Form/Extension/Core/Type/DateTimeTypeTest.php
@@ -175,4 +175,25 @@ public function testSubmit_stringSingleText()
$this->assertEquals('2010-06-02 03:04:00', $form->getData());
$this->assertEquals('2010-06-02 03:04:00', $form->getClientData());
}
+
+ /**
+ * @expectedException Symfony\Component\Form\Exception\FormException
+ */
+ public function testDifferentWidgets()
+ {
+ $form = $this->factory->create('datetime', null, array(
+ 'date_widget' => 'single_text',
+ 'time_widget' => 'choice',
+ ));
+ }
+
+ /**
+ * @expectedException Symfony\Component\Form\Exception\FormException
+ */
+ public function testDefinedOnlyOneWidget()
+ {
+ $form = $this->factory->create('datetime', null, array(
+ 'date_widget' => 'single_text',
+ ));
+ }
}

0 comments on commit 3c2539f

Please sign in to comment.
Something went wrong with that request. Please try again.