merged branch bschussek/issue4615 (PR #4850)
Commits
-------
92abf5a
[Form] Enabled error bubbling from the parts of a date/time field to the main field
Discussion
----------
[Form] Enabled error bubbling from the parts of a date/time field to the main field
Bug fix: yes
Feature addition: no
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: #4615
Todo: -
This commit is contained in:
commit
09d71ca927
@ -66,7 +66,9 @@ class DateType extends AbstractType
|
|||||||
$pattern
|
$pattern
|
||||||
));
|
));
|
||||||
} else {
|
} else {
|
||||||
$yearOptions = $monthOptions = $dayOptions = array();
|
$yearOptions = $monthOptions = $dayOptions = array(
|
||||||
|
'error_bubbling' => true,
|
||||||
|
);
|
||||||
|
|
||||||
$formatter = new \IntlDateFormatter(
|
$formatter = new \IntlDateFormatter(
|
||||||
\Locale::getDefault(),
|
\Locale::getDefault(),
|
||||||
@ -80,18 +82,12 @@ class DateType extends AbstractType
|
|||||||
|
|
||||||
if ('choice' === $options['widget']) {
|
if ('choice' === $options['widget']) {
|
||||||
// Only pass a subset of the options to children
|
// Only pass a subset of the options to children
|
||||||
$yearOptions = array(
|
$yearOptions['choices'] = $this->formatTimestamps($formatter, '/y+/', $this->listYears($options['years']));
|
||||||
'choices' => $this->formatTimestamps($formatter, '/y+/', $this->listYears($options['years'])),
|
$yearOptions['empty_value'] = $options['empty_value']['year'];
|
||||||
'empty_value' => $options['empty_value']['year'],
|
$monthOptions['choices'] = $this->formatTimestamps($formatter, '/M+/', $this->listMonths($options['months']));
|
||||||
);
|
$monthOptions['empty_value'] = $options['empty_value']['month'];
|
||||||
$monthOptions = array(
|
$dayOptions['choices'] = $this->formatTimestamps($formatter, '/d+/', $this->listDays($options['days']));
|
||||||
'choices' => $this->formatTimestamps($formatter, '/M+/', $this->listMonths($options['months'])),
|
$dayOptions['empty_value'] = $options['empty_value']['day'];
|
||||||
'empty_value' => $options['empty_value']['month'],
|
|
||||||
);
|
|
||||||
$dayOptions = array(
|
|
||||||
'choices' => $this->formatTimestamps($formatter, '/d+/', $this->listDays($options['days'])),
|
|
||||||
'empty_value' => $options['empty_value']['day'],
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Append generic carry-along options
|
// Append generic carry-along options
|
||||||
|
@ -39,7 +39,9 @@ class TimeType extends AbstractType
|
|||||||
if ('single_text' === $options['widget']) {
|
if ('single_text' === $options['widget']) {
|
||||||
$builder->addViewTransformer(new DateTimeToStringTransformer($options['model_timezone'], $options['view_timezone'], $format));
|
$builder->addViewTransformer(new DateTimeToStringTransformer($options['model_timezone'], $options['view_timezone'], $format));
|
||||||
} else {
|
} else {
|
||||||
$hourOptions = $minuteOptions = $secondOptions = array();
|
$hourOptions = $minuteOptions = $secondOptions = array(
|
||||||
|
'error_bubbling' => true,
|
||||||
|
);
|
||||||
|
|
||||||
if ('choice' === $options['widget']) {
|
if ('choice' === $options['widget']) {
|
||||||
$hours = $minutes = array();
|
$hours = $minutes = array();
|
||||||
@ -52,14 +54,10 @@ class TimeType extends AbstractType
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Only pass a subset of the options to children
|
// Only pass a subset of the options to children
|
||||||
$hourOptions = array(
|
$hourOptions['choices'] = $hours;
|
||||||
'choices' => $hours,
|
$hourOptions['empty_value'] = $options['empty_value']['hour'];
|
||||||
'empty_value' => $options['empty_value']['hour'],
|
$minuteOptions['choices'] = $minutes;
|
||||||
);
|
$minuteOptions['empty_value'] = $options['empty_value']['minute'];
|
||||||
$minuteOptions = array(
|
|
||||||
'choices' => $minutes,
|
|
||||||
'empty_value' => $options['empty_value']['minute'],
|
|
||||||
);
|
|
||||||
|
|
||||||
if ($options['with_seconds']) {
|
if ($options['with_seconds']) {
|
||||||
$seconds = array();
|
$seconds = array();
|
||||||
@ -68,10 +66,8 @@ class TimeType extends AbstractType
|
|||||||
$seconds[$second] = str_pad($second, 2, '0', STR_PAD_LEFT);
|
$seconds[$second] = str_pad($second, 2, '0', STR_PAD_LEFT);
|
||||||
}
|
}
|
||||||
|
|
||||||
$secondOptions = array(
|
$secondOptions['choices'] = $seconds;
|
||||||
'choices' => $seconds,
|
$secondOptions['empty_value'] = $options['empty_value']['second'];
|
||||||
'empty_value' => $options['empty_value']['second'],
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Append generic carry-along options
|
// Append generic carry-along options
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
namespace Symfony\Component\Form\Tests\Extension\Core\Type;
|
namespace Symfony\Component\Form\Tests\Extension\Core\Type;
|
||||||
|
|
||||||
use Symfony\Component\Form\Extension\Core\View\ChoiceView;
|
use Symfony\Component\Form\Extension\Core\View\ChoiceView;
|
||||||
|
use Symfony\Component\Form\FormError;
|
||||||
|
|
||||||
class DateTypeTest extends LocalizedTestCase
|
class DateTypeTest extends LocalizedTestCase
|
||||||
{
|
{
|
||||||
@ -686,4 +687,57 @@ class DateTypeTest extends LocalizedTestCase
|
|||||||
$view = $form->createView();
|
$view = $form->createView();
|
||||||
$this->assertNull($view->getVar('type'));
|
$this->assertNull($view->getVar('type'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function provideCompoundWidgets()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
array('text'),
|
||||||
|
array('choice'),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider provideCompoundWidgets
|
||||||
|
*/
|
||||||
|
public function testYearErrorsBubbleUp($widget)
|
||||||
|
{
|
||||||
|
$error = new FormError('Invalid!');
|
||||||
|
$form = $this->factory->create('date', null, array(
|
||||||
|
'widget' => $widget,
|
||||||
|
));
|
||||||
|
$form->get('year')->addError($error);
|
||||||
|
|
||||||
|
$this->assertSame(array(), $form->get('year')->getErrors());
|
||||||
|
$this->assertSame(array($error), $form->getErrors());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider provideCompoundWidgets
|
||||||
|
*/
|
||||||
|
public function testMonthErrorsBubbleUp($widget)
|
||||||
|
{
|
||||||
|
$error = new FormError('Invalid!');
|
||||||
|
$form = $this->factory->create('date', null, array(
|
||||||
|
'widget' => $widget,
|
||||||
|
));
|
||||||
|
$form->get('month')->addError($error);
|
||||||
|
|
||||||
|
$this->assertSame(array(), $form->get('month')->getErrors());
|
||||||
|
$this->assertSame(array($error), $form->getErrors());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider provideCompoundWidgets
|
||||||
|
*/
|
||||||
|
public function testDayErrorsBubbleUp($widget)
|
||||||
|
{
|
||||||
|
$error = new FormError('Invalid!');
|
||||||
|
$form = $this->factory->create('date', null, array(
|
||||||
|
'widget' => $widget,
|
||||||
|
));
|
||||||
|
$form->get('day')->addError($error);
|
||||||
|
|
||||||
|
$this->assertSame(array(), $form->get('day')->getErrors());
|
||||||
|
$this->assertSame(array($error), $form->getErrors());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
namespace Symfony\Component\Form\Tests\Extension\Core\Type;
|
namespace Symfony\Component\Form\Tests\Extension\Core\Type;
|
||||||
|
|
||||||
use Symfony\Component\Form\Extension\Core\View\ChoiceView;
|
use Symfony\Component\Form\Extension\Core\View\ChoiceView;
|
||||||
|
use Symfony\Component\Form\FormError;
|
||||||
|
|
||||||
class TimeTypeTest extends LocalizedTestCase
|
class TimeTypeTest extends LocalizedTestCase
|
||||||
{
|
{
|
||||||
@ -506,4 +507,58 @@ class TimeTypeTest extends LocalizedTestCase
|
|||||||
$this->assertNull($view->get('minute')->getVar('empty_value'));
|
$this->assertNull($view->get('minute')->getVar('empty_value'));
|
||||||
$this->assertSame('Empty second', $view->get('second')->getVar('empty_value'));
|
$this->assertSame('Empty second', $view->get('second')->getVar('empty_value'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function provideCompoundWidgets()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
array('text'),
|
||||||
|
array('choice'),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider provideCompoundWidgets
|
||||||
|
*/
|
||||||
|
public function testHourErrorsBubbleUp($widget)
|
||||||
|
{
|
||||||
|
$error = new FormError('Invalid!');
|
||||||
|
$form = $this->factory->create('time', null, array(
|
||||||
|
'widget' => $widget,
|
||||||
|
));
|
||||||
|
$form->get('hour')->addError($error);
|
||||||
|
|
||||||
|
$this->assertSame(array(), $form->get('hour')->getErrors());
|
||||||
|
$this->assertSame(array($error), $form->getErrors());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider provideCompoundWidgets
|
||||||
|
*/
|
||||||
|
public function testMinuteErrorsBubbleUp($widget)
|
||||||
|
{
|
||||||
|
$error = new FormError('Invalid!');
|
||||||
|
$form = $this->factory->create('time', null, array(
|
||||||
|
'widget' => $widget,
|
||||||
|
));
|
||||||
|
$form->get('minute')->addError($error);
|
||||||
|
|
||||||
|
$this->assertSame(array(), $form->get('minute')->getErrors());
|
||||||
|
$this->assertSame(array($error), $form->getErrors());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider provideCompoundWidgets
|
||||||
|
*/
|
||||||
|
public function testSecondErrorsBubbleUp($widget)
|
||||||
|
{
|
||||||
|
$error = new FormError('Invalid!');
|
||||||
|
$form = $this->factory->create('time', null, array(
|
||||||
|
'widget' => $widget,
|
||||||
|
'with_seconds' => true,
|
||||||
|
));
|
||||||
|
$form->get('second')->addError($error);
|
||||||
|
|
||||||
|
$this->assertSame(array(), $form->get('second')->getErrors());
|
||||||
|
$this->assertSame(array($error), $form->getErrors());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user