ChoiceView example


        IntlTestHelper::requireIntl($this, false);

        parent::setUp();
    }

    public function testLocalesAreSelectable()
    {
        $choices = $this->factory->create(static::TESTED_TYPE)
            ->createView()->vars['choices'];

        $this->assertContainsEquals(new ChoiceView('en', 'en', 'English')$choices);
        $this->assertContainsEquals(new ChoiceView('en_GB', 'en_GB', 'English (United Kingdom)')$choices);
        $this->assertContainsEquals(new ChoiceView('zh_Hant_HK', 'zh_Hant_HK', 'Chinese (Traditional, Hong Kong SAR China)')$choices);
    }

    /** * @requires extension intl */
    public function testChoiceTranslationLocaleOption()
    {
        $choices = $this->factory
            ->create(static::TESTED_TYPE, null, [
                

        IntlTestHelper::requireIntl($this, false);

        parent::setUp();
    }

    public function testCurrenciesAreSelectable()
    {
        $choices = $this->factory->create(static::TESTED_TYPE)
            ->createView()->vars['choices'];

        $this->assertContainsEquals(new ChoiceView('EUR', 'EUR', 'Euro')$choices);
        $this->assertContainsEquals(new ChoiceView('USD', 'USD', 'US Dollar')$choices);
        $this->assertContainsEquals(new ChoiceView('SIT', 'SIT', 'Slovenian Tolar')$choices);
    }

    /** * @requires extension intl */
    public function testChoiceTranslationLocaleOption()
    {
        $choices = $this->factory
            ->create(static::TESTED_TYPE, null, [
                
use Symfony\Component\Intl\Util\IntlTestHelper;

class TimezoneTypeTest extends BaseTypeTestCase
{
    public const TESTED_TYPE = 'Symfony\Component\Form\Extension\Core\Type\TimezoneType';

    public function testTimezonesAreSelectable()
    {
        $choices = $this->factory->create(static::TESTED_TYPE)
            ->createView()->vars['choices'];

        $this->assertContainsEquals(new ChoiceView('Africa/Kinshasa', 'Africa/Kinshasa', 'Africa / Kinshasa')$choices);
        $this->assertContainsEquals(new ChoiceView('America/New_York', 'America/New_York', 'America / New York')$choices);
    }

    public function testSubmitNull($expected = null, $norm = null, $view = null)
    {
        parent::testSubmitNull($expected$norm, '');
    }

    public function testSubmitNullUsesDefaultEmptyData($emptyData = 'Africa/Kinshasa', $expectedData = 'Africa/Kinshasa')
    {
        $form = $this->factory->create(static::TESTED_TYPE, null, [
            
            $dynamicLabel = $label($choice$key$value);

            if (false === $dynamicLabel) {
                $label = false;
            } elseif ($dynamicLabel instanceof TranslatableInterface) {
                $label = $dynamicLabel;
            } else {
                $label = (string) $dynamicLabel;
            }
        }

        $view = new ChoiceView(
            $choice,
            $value,
            $label,
            // The attributes may be a callable or a mapping from choice indices             // to nested arrays             \is_callable($attr) ? $attr($choice$key$value) : ($attr[$key] ?? []),
            // The label translation parameters may be a callable or a mapping from choice indices             // to nested arrays             \is_callable($labelTranslationParameters) ? $labelTranslationParameters($choice$key$value) : ($labelTranslationParameters[$key] ?? [])
        );

        


    public function testPassChoicesToView()
    {
        $choices = ['A' => 'a', 'B' => 'b', 'C' => 'c', 'D' => 'd'];
        $view = $this->factory->create(static::TESTED_TYPE, null, [
            'choices' => $choices,
        ])
            ->createView();

        $this->assertEquals([
            new ChoiceView('a', 'a', 'A'),
            new ChoiceView('b', 'b', 'B'),
            new ChoiceView('c', 'c', 'C'),
            new ChoiceView('d', 'd', 'D'),
        ]$view->vars['choices']);
    }

    public function testPassPreferredChoicesToView()
    {
        $choices = ['A' => 'a', 'B' => 'b', 'C' => 'c', 'D' => 'd'];
        $view = $this->factory->create(static::TESTED_TYPE, null, [
            'choices' => $choices,
            
$object = (object) ['property' => 'value'];
        $loader = new ArrayChoiceLoader([$object]);

        $this->assertSame(['value' => $object]$this->factory->createListFromLoader($loadernew PropertyPath('property'))->getChoices());
    }

    public function testCreateViewPreferredChoicesAsPropertyPath()
    {
        $object = (object) ['preferred_choice' => true, 'view_label' => 'foo', 'view_index' => 'key', 'view_group' => 'bar', 'view_attribute' => ['baz' => 'foobar']];
        $list = new ArrayChoiceList([$object]);

        $this->assertEquals([new ChoiceView($object, '0', '0')]$this->factory->createView($list, 'preferred_choice')->choices);
        $this->assertEquals([new ChoiceView($object, '0', '0')]$this->factory->createView($list, 'preferred_choice')->preferredChoices);
    }

    public function testCreateViewPreferredChoicesAsPropertyPathInstance()
    {
        $object = (object) ['preferred_choice' => true, 'view_label' => 'foo', 'view_index' => 'key', 'view_group' => 'bar', 'view_attribute' => ['baz' => 'foobar']];
        $list = new ArrayChoiceList([$object]);

        $this->assertEquals([new ChoiceView($object, '0', '0')]$this->factory->createView($listnew PropertyPath('preferred_choice'))->choices);
        $this->assertEquals([new ChoiceView($object, '0', '0')]$this->factory->createView($listnew PropertyPath('preferred_choice'))->preferredChoices);
    }

    

        IntlTestHelper::requireIntl($this, false);

        parent::setUp();
    }

    public function testCountriesAreSelectable()
    {
        $choices = $this->factory->create(static::TESTED_TYPE)
            ->createView()->vars['choices'];

        $this->assertContainsEquals(new ChoiceView('en', 'en', 'English')$choices);
        $this->assertContainsEquals(new ChoiceView('fr', 'fr', 'French')$choices);
        $this->assertContainsEquals(new ChoiceView('my', 'my', 'Burmese')$choices);
    }

    /** * @requires extension intl */
    public function testChoiceTranslationLocaleOption()
    {
        $choices = $this->factory
            ->create(static::TESTED_TYPE, null, [
                
false, 10, [1, 'foo', 'foo10']],
            [false, 0, [1, 'foo', 'foo10']],
        ];
    }

    /** * @dataProvider isSelectedChoiceProvider */
    public function testIsChoiceSelected($expected$choice$value)
    {
        $choice = new ChoiceView($choice$choice$choice.' label');

        $this->assertSame($expected, \Symfony\Bridge\Twig\Extension\twig_is_selected_choice($choice$value));
    }

    public function testStartTagHasNoActionAttributeWhenActionIsEmpty()
    {
        $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\FormType', null, [
            'method' => 'get',
            'action' => '',
        ]);

        
            // initialized choice lists. For example, when using an SQL driver,             // the index check would read in one SQL query and the initialization             // requires another SQL query. When the initialization is done first,             // one SQL query is sufficient.
            $choiceListView = $this->createChoiceListView($choiceList$options);
            $builder->setAttribute('choice_list_view', $choiceListView);

            // Check if the choices already contain the empty value             // Only add the placeholder option if this is not the case             if (null !== $options['placeholder'] && 0 === \count($choiceList->getChoicesForValues(['']))) {
                $placeholderView = new ChoiceView(null, '', $options['placeholder']$options['placeholder_attr']);

                // "placeholder" is a reserved name                 $this->addSubForm($builder, 'placeholder', $placeholderView$options);
            }

            $this->addSubForms($builder$choiceListView->preferredChoices, $options);
            $this->addSubForms($builder$choiceListView->choices, $options);
        }

        if ($options['expanded'] || $options['multiple']) {
            // Make sure that scalar, submitted values are converted to arrays
public function testYearsOption()
    {
        $form = $this->factory->create(static::TESTED_TYPE, null, [
            'years' => [2010, 2011],
            'widget' => 'choice',
        ]);

        $view = $form->createView();

        $this->assertEquals([
            new ChoiceView('2010', '2010', '2010'),
            new ChoiceView('2011', '2011', '2011'),
        ]$view['year']->vars['choices']);
    }

    public function testMonthsOption()
    {
        \Locale::setDefault('en');
        $form = $this->factory->create(static::TESTED_TYPE, null, [
            'months' => [6, 7],
            'format' => \IntlDateFormatter::SHORT,
            'widget' => 'choice',
        ]);
public function testHoursOption()
    {
        $form = $this->factory->create(static::TESTED_TYPE, null, [
            'hours' => [6, 7],
            'widget' => 'choice',
        ]);

        $view = $form->createView();

        $this->assertEquals([
            new ChoiceView('6', '6', '06'),
            new ChoiceView('7', '7', '07'),
        ]$view['hour']->vars['choices']);
    }

    public function testIsMinuteWithinRangeReturnsTrueIfWithin()
    {
        $form = $this->factory->create(static::TESTED_TYPE, null, [
            'minutes' => [6, 7],
            'widget' => 'choice',
        ]);

        
IntlTestHelper::requireIntl($this, false);

        parent::setUp();
    }

    public function testCountriesAreSelectable()
    {
        $choices = $this->factory->create(static::TESTED_TYPE)
            ->createView()->vars['choices'];

        // Don't check objects for identity         $this->assertContainsEquals(new ChoiceView('DE', 'DE', 'Germany')$choices);
        $this->assertContainsEquals(new ChoiceView('GB', 'GB', 'United Kingdom')$choices);
        $this->assertContainsEquals(new ChoiceView('US', 'US', 'United States')$choices);
        $this->assertContainsEquals(new ChoiceView('FR', 'FR', 'France')$choices);
        $this->assertContainsEquals(new ChoiceView('MY', 'MY', 'Malaysia')$choices);
    }

    /** * @requires extension intl */
    public function testChoiceTranslationLocaleOption()
    {
        
$entity2 = new SingleIntIdEntity(2, 'Bar');

        $this->persist([$entity1$entity2]);

        $field = $this->factory->createNamed('name', static::TESTED_TYPE, null, [
            'em' => 'default',
            'class' => self::SINGLE_IDENT_CLASS,
            'required' => false,
            'choice_label' => 'name',
        ]);

        $this->assertEquals([1 => new ChoiceView($entity1, '1', 'Foo'), 2 => new ChoiceView($entity2, '2', 'Bar')]$field->createView()->vars['choices']);
    }

    public function testSetDataToUninitializedEntityWithNonRequiredToString()
    {
        $entity1 = new SingleIntIdEntity(1, 'Foo');
        $entity2 = new SingleIntIdEntity(2, 'Bar');

        $this->persist([$entity1$entity2]);

        $view = $this->factory->createNamed('name', static::TESTED_TYPE, null, [
            'em' => 'default',
            
$list = $this->factory->createListFromLoader(new ArrayChoiceLoader(), null, $filter);

        $this->assertEquals(new LazyChoiceList(new FilterChoiceLoaderDecorator(new ArrayChoiceLoader()$filter))$list);
    }

    public function testCreateViewFlat()
    {
        $view = $this->factory->createView($this->list);

        $this->assertEquals(new ChoiceListView(
            [
                0 => new ChoiceView($this->obj1, '0', 'A'),
                1 => new ChoiceView($this->obj2, '1', 'B'),
                2 => new ChoiceView($this->obj3, '2', 'C'),
                3 => new ChoiceView($this->obj4, '3', 'D'),
            ][]
        )$view);
    }

    public function testCreateViewFlatPreferredChoices()
    {
        $view = $this->factory->createView(
            $this->list,
            [
Home | Imprint | This part of the site doesn't use cookies.