Skip to content

Commit 92abf5a

Browse files
committed
[Form] Enabled error bubbling from the parts of a date/time field to the main field
1 parent f3547d4 commit 92abf5a

File tree

4 files changed

+127
-26
lines changed

4 files changed

+127
-26
lines changed

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

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ public function buildForm(FormBuilderInterface $builder, array $options)
6666
$pattern
6767
));
6868
} else {
69-
$yearOptions = $monthOptions = $dayOptions = array();
69+
$yearOptions = $monthOptions = $dayOptions = array(
70+
'error_bubbling' => true,
71+
);
7072

7173
$formatter = new \IntlDateFormatter(
7274
\Locale::getDefault(),
@@ -80,18 +82,12 @@ public function buildForm(FormBuilderInterface $builder, array $options)
8082

8183
if ('choice' === $options['widget']) {
8284
// Only pass a subset of the options to children
83-
$yearOptions = array(
84-
'choices' => $this->formatTimestamps($formatter, '/y+/', $this->listYears($options['years'])),
85-
'empty_value' => $options['empty_value']['year'],
86-
);
87-
$monthOptions = array(
88-
'choices' => $this->formatTimestamps($formatter, '/M+/', $this->listMonths($options['months'])),
89-
'empty_value' => $options['empty_value']['month'],
90-
);
91-
$dayOptions = array(
92-
'choices' => $this->formatTimestamps($formatter, '/d+/', $this->listDays($options['days'])),
93-
'empty_value' => $options['empty_value']['day'],
94-
);
85+
$yearOptions['choices'] = $this->formatTimestamps($formatter, '/y+/', $this->listYears($options['years']));
86+
$yearOptions['empty_value'] = $options['empty_value']['year'];
87+
$monthOptions['choices'] = $this->formatTimestamps($formatter, '/M+/', $this->listMonths($options['months']));
88+
$monthOptions['empty_value'] = $options['empty_value']['month'];
89+
$dayOptions['choices'] = $this->formatTimestamps($formatter, '/d+/', $this->listDays($options['days']));
90+
$dayOptions['empty_value'] = $options['empty_value']['day'];
9591
}
9692

9793
// Append generic carry-along options

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

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ public function buildForm(FormBuilderInterface $builder, array $options)
3939
if ('single_text' === $options['widget']) {
4040
$builder->addViewTransformer(new DateTimeToStringTransformer($options['model_timezone'], $options['view_timezone'], $format));
4141
} else {
42-
$hourOptions = $minuteOptions = $secondOptions = array();
42+
$hourOptions = $minuteOptions = $secondOptions = array(
43+
'error_bubbling' => true,
44+
);
4345

4446
if ('choice' === $options['widget']) {
4547
$hours = $minutes = array();
@@ -52,14 +54,10 @@ public function buildForm(FormBuilderInterface $builder, array $options)
5254
}
5355

5456
// Only pass a subset of the options to children
55-
$hourOptions = array(
56-
'choices' => $hours,
57-
'empty_value' => $options['empty_value']['hour'],
58-
);
59-
$minuteOptions = array(
60-
'choices' => $minutes,
61-
'empty_value' => $options['empty_value']['minute'],
62-
);
57+
$hourOptions['choices'] = $hours;
58+
$hourOptions['empty_value'] = $options['empty_value']['hour'];
59+
$minuteOptions['choices'] = $minutes;
60+
$minuteOptions['empty_value'] = $options['empty_value']['minute'];
6361

6462
if ($options['with_seconds']) {
6563
$seconds = array();
@@ -68,10 +66,8 @@ public function buildForm(FormBuilderInterface $builder, array $options)
6866
$seconds[$second] = str_pad($second, 2, '0', STR_PAD_LEFT);
6967
}
7068

71-
$secondOptions = array(
72-
'choices' => $seconds,
73-
'empty_value' => $options['empty_value']['second'],
74-
);
69+
$secondOptions['choices'] = $seconds;
70+
$secondOptions['empty_value'] = $options['empty_value']['second'];
7571
}
7672

7773
// Append generic carry-along options

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Form\Tests\Extension\Core\Type;
1313

1414
use Symfony\Component\Form\Extension\Core\View\ChoiceView;
15+
use Symfony\Component\Form\FormError;
1516

1617
class DateTypeTest extends LocalizedTestCase
1718
{
@@ -686,4 +687,57 @@ public function testDontPassHtml5TypeIfNotSingleText()
686687
$view = $form->createView();
687688
$this->assertNull($view->getVar('type'));
688689
}
690+
691+
public function provideCompoundWidgets()
692+
{
693+
return array(
694+
array('text'),
695+
array('choice'),
696+
);
697+
}
698+
699+
/**
700+
* @dataProvider provideCompoundWidgets
701+
*/
702+
public function testYearErrorsBubbleUp($widget)
703+
{
704+
$error = new FormError('Invalid!');
705+
$form = $this->factory->create('date', null, array(
706+
'widget' => $widget,
707+
));
708+
$form->get('year')->addError($error);
709+
710+
$this->assertSame(array(), $form->get('year')->getErrors());
711+
$this->assertSame(array($error), $form->getErrors());
712+
}
713+
714+
/**
715+
* @dataProvider provideCompoundWidgets
716+
*/
717+
public function testMonthErrorsBubbleUp($widget)
718+
{
719+
$error = new FormError('Invalid!');
720+
$form = $this->factory->create('date', null, array(
721+
'widget' => $widget,
722+
));
723+
$form->get('month')->addError($error);
724+
725+
$this->assertSame(array(), $form->get('month')->getErrors());
726+
$this->assertSame(array($error), $form->getErrors());
727+
}
728+
729+
/**
730+
* @dataProvider provideCompoundWidgets
731+
*/
732+
public function testDayErrorsBubbleUp($widget)
733+
{
734+
$error = new FormError('Invalid!');
735+
$form = $this->factory->create('date', null, array(
736+
'widget' => $widget,
737+
));
738+
$form->get('day')->addError($error);
739+
740+
$this->assertSame(array(), $form->get('day')->getErrors());
741+
$this->assertSame(array($error), $form->getErrors());
742+
}
689743
}

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Form\Tests\Extension\Core\Type;
1313

1414
use Symfony\Component\Form\Extension\Core\View\ChoiceView;
15+
use Symfony\Component\Form\FormError;
1516

1617
class TimeTypeTest extends LocalizedTestCase
1718
{
@@ -506,4 +507,58 @@ public function testPassEmptyValueAsPartialArray_addNullIfRequired()
506507
$this->assertNull($view->get('minute')->getVar('empty_value'));
507508
$this->assertSame('Empty second', $view->get('second')->getVar('empty_value'));
508509
}
510+
511+
public function provideCompoundWidgets()
512+
{
513+
return array(
514+
array('text'),
515+
array('choice'),
516+
);
517+
}
518+
519+
/**
520+
* @dataProvider provideCompoundWidgets
521+
*/
522+
public function testHourErrorsBubbleUp($widget)
523+
{
524+
$error = new FormError('Invalid!');
525+
$form = $this->factory->create('time', null, array(
526+
'widget' => $widget,
527+
));
528+
$form->get('hour')->addError($error);
529+
530+
$this->assertSame(array(), $form->get('hour')->getErrors());
531+
$this->assertSame(array($error), $form->getErrors());
532+
}
533+
534+
/**
535+
* @dataProvider provideCompoundWidgets
536+
*/
537+
public function testMinuteErrorsBubbleUp($widget)
538+
{
539+
$error = new FormError('Invalid!');
540+
$form = $this->factory->create('time', null, array(
541+
'widget' => $widget,
542+
));
543+
$form->get('minute')->addError($error);
544+
545+
$this->assertSame(array(), $form->get('minute')->getErrors());
546+
$this->assertSame(array($error), $form->getErrors());
547+
}
548+
549+
/**
550+
* @dataProvider provideCompoundWidgets
551+
*/
552+
public function testSecondErrorsBubbleUp($widget)
553+
{
554+
$error = new FormError('Invalid!');
555+
$form = $this->factory->create('time', null, array(
556+
'widget' => $widget,
557+
'with_seconds' => true,
558+
));
559+
$form->get('second')->addError($error);
560+
561+
$this->assertSame(array(), $form->get('second')->getErrors());
562+
$this->assertSame(array($error), $form->getErrors());
563+
}
509564
}

0 commit comments

Comments
 (0)