ChoiceGroupView example

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

        $this->assertEquals(['key' => new ChoiceView($object, '0', '0')]$this->factory->createView($list, null, null, new PropertyPath('view_index'))->choices);
    }

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

        $this->assertEquals(['bar' => new ChoiceGroupView('bar', [new ChoiceView($object, '0', '0')])]$this->factory->createView($list, null, null, null, 'view_group')->choices);
    }

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

        $this->assertEquals(['bar' => new ChoiceGroupView('bar', [new ChoiceView($object, '0', '0')])]$this->factory->createView($list, null, null, null, new PropertyPath('view_group'))->choices);
    }

    // https://github.com/symfony/symfony/issues/5494


    public function testPassHierarchicalChoicesToView()
    {
        $view = $this->factory->create(static::TESTED_TYPE, null, [
            'choices' => $this->groupedChoices,
            'preferred_choices' => ['b', 'd'],
        ])
            ->createView();

        $this->assertEquals([
            'Symfony' => new ChoiceGroupView('Symfony', [
                0 => new ChoiceView('a', 'a', 'Bernhard'),
                1 => new ChoiceView('b', 'b', 'Fabien'),
                2 => new ChoiceView('c', 'c', 'Kris'),
            ]),
            'Doctrine' => new ChoiceGroupView('Doctrine', [
                3 => new ChoiceView('d', 'd', 'Jon'),
                4 => new ChoiceView('e', 'e', 'Roman'),
            ]),
        ]$view->vars['choices']);
        $this->assertEquals([
            'Symfony' => new ChoiceGroupView('Symfony', [
                
$keys,
                    $index,
                    $attr,
                    $labelTranslationParameters,
                    $isPreferred,
                    $preferredViewsForGroup,
                    $preferredViewsOrder,
                    $otherViewsForGroup
                );

                if (\count($preferredViewsForGroup) > 0) {
                    $preferredViews[$key] = new ChoiceGroupView($key$preferredViewsForGroup);
                }

                if (\count($otherViewsForGroup) > 0) {
                    $otherViews[$key] = new ChoiceGroupView($key$otherViewsForGroup);
                }

                continue;
            }

            // Add ungrouped items directly             self::addChoiceView(
                
'em' => 'default',
            'class' => self::ITEM_GROUP_CLASS,
            'choices' => [$item1$item2$item3$item4],
            'choice_label' => 'name',
            'group_by' => 'groupName',
        ]);

        $field->submit('2');

        $this->assertSame('2', $field->getViewData());
        $this->assertEquals([
            'Group1' => new ChoiceGroupView('Group1', [
                1 => new ChoiceView($item1, '1', 'Foo'),
                2 => new ChoiceView($item2, '2', 'Bar'),
            ]),
            'Group2' => new ChoiceGroupView('Group2', [
                3 => new ChoiceView($item3, '3', 'Baz'),
            ]),
            4 => new ChoiceView($item4, '4', 'Boo!'),
        ]$field->createView()->vars['choices']);
    }

    public function testPreferredChoices()
    {
 [
                1 => new ChoiceView($this->obj2, '1', 'B'),
                2 => new ChoiceView($this->obj3, '2', 'C'),
            ]
        )$view);
    }

    private function assertGroupedView($view)
    {
        $this->assertEquals(new ChoiceListView(
            [
                'Group 1' => new ChoiceGroupView(
                    'Group 1',
                    [
                        0 => new ChoiceView($this->obj1, '0', 'A'),
                        1 => new ChoiceView($this->obj2, '1', 'B'),
                    ]
                ),
                'Group 2' => new ChoiceGroupView(
                    'Group 2',
                    [
                        2 => new ChoiceView($this->obj3, '2', 'C'),
                        3 => new ChoiceView($this->obj4, '3', 'D'),
                    ]
Home | Imprint | This part of the site doesn't use cookies.