Merge remote branch 'weaverryan/form_tweaks'

* weaverryan/form_tweaks:
  [Form] Removing an unused variable in TimeType
  [Form] Throwing exception for invalid "input" type of DateType
  [Form] Removing unused private property and adding PHPDoc
  [Form] Fixing wrong variable name
  [Form] Tweak to FileTypeTest to avoid warning
  [Form] Adding an exception for an invalid widget option in DateType
This commit is contained in:
Fabien Potencier 2011-04-24 08:45:44 +02:00
commit 27ae0a341d
6 changed files with 36 additions and 6 deletions

View File

@ -99,7 +99,7 @@ abstract class AbstractExtension implements FormExtensionInterface
$guesser = $this->loadTypeGuesser();
if (!$guesser instanceof FormTypeGuesserInterface) {
throw new UnexpectedTypeException($type, 'Symfony\Component\Form\FormTypeGuesserInterface');
throw new UnexpectedTypeException($guesser, 'Symfony\Component\Form\FormTypeGuesserInterface');
}
$this->guesser = $guesser;

View File

@ -16,12 +16,15 @@ use Symfony\Component\Form\AbstractExtension;
use Symfony\Component\Validator\ValidatorInterface;
use Symfony\Component\HttpFoundation\File\TemporaryStorage;
/**
* Represents the main form extension, which loads the core functionality.
*
* @author Bernhard Schussek <bernhard.schussek@symfony.com>
*/
class CoreExtension extends AbstractExtension
{
private $storage;
private $typeGuesser;
public function __construct(TemporaryStorage $storage)
{
$this->storage = $storage;

View File

@ -14,6 +14,7 @@ namespace Symfony\Component\Form\Extension\Core\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\Exception\FormException;
use Symfony\Component\Form\Extension\Core\ChoiceList\PaddedChoiceList;
use Symfony\Component\Form\Extension\Core\ChoiceList\MonthChoiceList;
use Symfony\Component\Form\FormView;
@ -36,7 +37,7 @@ class DateType extends AbstractType
if ($options['widget'] === 'text') {
$builder->appendClientTransformer(new DateTimeToLocalizedStringTransformer($options['data_timezone'], $options['user_timezone'], $options['format'], \IntlDateFormatter::NONE));
} else {
} elseif ($options['widget'] == 'choice') {
// Only pass a subset of the options to children
$yearOptions = array(
'choice_list' => new PaddedChoiceList(
@ -58,6 +59,8 @@ class DateType extends AbstractType
->add('month', 'choice', $monthOptions)
->add('day', 'choice', $dayOptions)
->appendClientTransformer(new DateTimeToArrayTransformer($options['data_timezone'], $options['user_timezone'], array('year', 'month', 'day')));
} else {
throw new FormException('The "widget" option must be set to either "text" or "choice".');
}
if ($options['input'] === 'string') {
@ -72,6 +75,8 @@ class DateType extends AbstractType
$builder->appendNormTransformer(new ReversedTransformer(
new DateTimeToArrayTransformer($options['data_timezone'], $options['data_timezone'], array('year', 'month', 'day'))
));
} else if ($options['input'] !== 'datetime') {
throw new FormException('The "input" option must be "datetime", "string", "timestamp" or "array".');
}
$builder

View File

@ -26,7 +26,6 @@ class TimeType extends AbstractType
public function buildForm(FormBuilder $builder, array $options)
{
$hourOptions = $minuteOptions = $secondOptions = array();
$child = $options['widget'] === 'text' ? 'text' : 'choice';
$parts = array('hour', 'minute');
if ($options['widget'] === 'choice') {

View File

@ -25,6 +25,26 @@ class DateTypeTest extends LocalizedTestCase
\Locale::setDefault('de_AT');
}
/**
* @expectedException Symfony\Component\Form\Exception\FormException
*/
public function testInvalidWidgetOption()
{
$form = $this->factory->create('date', null, array(
'widget' => 'fake_widget',
));
}
/**
* @expectedException Symfony\Component\Form\Exception\FormException
*/
public function testInvalidInputOption()
{
$form = $this->factory->create('date', null, array(
'input' => 'fake_input',
));
}
public function testSubmitFromInputDateTime()
{
$form = $this->factory->create('date', null, array(

View File

@ -25,7 +25,10 @@ class FileTypeTest extends TypeTestCase
public static function setUpBeforeClass()
{
self::$tmpDir = sys_get_temp_dir().DIRECTORY_SEPARATOR.'symfony-test';
mkdir(self::$tmpDir, 0777, true);
if (!file_exists(self::$tmpDir)) {
mkdir(self::$tmpDir, 0777, true);
}
}
protected function setUp()