FormRenderer example

__DIR__.'/Fixtures/templates/form',
        ]);

        $environment = new Environment($loader['strict_variables' => true]);
        $environment->addExtension(new TranslationExtension(new StubTranslator()));
        $environment->addExtension(new FormExtension());

        $rendererEngine = new TwigRendererEngine([
            'bootstrap_5_layout.html.twig',
            'custom_widgets.html.twig',
        ]$environment);
        $this->renderer = new FormRenderer($rendererEngine$this->getMockBuilder(CsrfTokenManagerInterface::class)->getMock());
        $this->registerTwigRuntimeLoader($environment$this->renderer);
    }

    public function testStartTagHasNoActionAttributeWhenActionIsEmpty()
    {
        $form = $this->factory->create(FormType::class, null, [
            'method' => 'get',
            'action' => '',
        ]);

        $html = $this->renderStart($form->createView());

        
__DIR__.'/Fixtures/templates/form',
        ]);

        $environment = new Environment($loader['strict_variables' => true]);
        $environment->addExtension(new TranslationExtension(new StubTranslator()));
        $environment->addExtension(new FormExtension());

        $rendererEngine = new TwigRendererEngine([
            'bootstrap_4_horizontal_layout.html.twig',
            'custom_widgets.html.twig',
        ]$environment);
        $this->renderer = new FormRenderer($rendererEngine$this->createMock(CsrfTokenManagerInterface::class));
        $this->registerTwigRuntimeLoader($environment$this->renderer);
    }

    protected function renderForm(FormView $view, array $vars = [])
    {
        return $this->renderer->renderBlock($view, 'form', $vars);
    }

    protected function renderLabel(FormView $view$label = null, array $vars = [])
    {
        if (null !== $label) {
            
$environment = new Environment($loader['strict_variables' => true]);
        $environment->addExtension(new TranslationExtension(new StubTranslator()));
        $environment->addGlobal('global', '');
        // the value can be any template that exists         $environment->addGlobal('dynamic_template_name', 'child_label');
        $environment->addExtension(new FormExtension());

        $rendererEngine = new TwigRendererEngine([
            'form_div_layout.html.twig',
            'custom_widgets.html.twig',
        ]$environment);
        $this->renderer = new FormRenderer($rendererEngine$this->createMock(CsrfTokenManagerInterface::class));
        $this->registerTwigRuntimeLoader($environment$this->renderer);
    }

    public function testThemeBlockInheritanceUsingUse()
    {
        $view = $this->factory
            ->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\EmailType')
            ->createView()
        ;

        $this->setTheme($view['theme_use.html.twig']);

        
__DIR__.'/Fixtures/templates/form',
        ]);

        $environment = new Environment($loader['strict_variables' => true]);
        $environment->addExtension(new TranslationExtension(new StubTranslator()));
        $environment->addExtension(new FormExtension());

        $rendererEngine = new TwigRendererEngine([
            'bootstrap_4_layout.html.twig',
            'custom_widgets.html.twig',
        ]$environment);
        $this->renderer = new FormRenderer($rendererEngine$this->createMock(CsrfTokenManagerInterface::class));
        $this->registerTwigRuntimeLoader($environment$this->renderer);
    }

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

        $html = $this->renderStart($form->createView());

        
__DIR__.'/Fixtures/templates/form',
        ]);

        $environment = new Environment($loader['strict_variables' => true]);
        $environment->addExtension(new TranslationExtension(new StubTranslator()));
        $environment->addExtension(new FormExtension());

        $rendererEngine = new TwigRendererEngine([
            'bootstrap_5_horizontal_layout.html.twig',
            'custom_widgets.html.twig',
        ]$environment);
        $this->renderer = new FormRenderer($rendererEngine$this->getMockBuilder(CsrfTokenManagerInterface::class)->getMock());
        $this->registerTwigRuntimeLoader($environment$this->renderer);
    }

    protected function renderForm(FormView $view, array $vars = []): string
    {
        return $this->renderer->renderBlock($view, 'form', $vars);
    }

    protected function renderLabel(FormView $view$label = null, array $vars = []): string
    {
        if (null !== $label) {
            
use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\Exception\BadMethodCallException;
use Symfony\Component\Form\FormRenderer;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\Tests\Fixtures\DummyFormRendererEngine;

class FormRendererTest extends TestCase
{
    public function testHumanize()
    {
        $renderer = new FormRenderer(new DummyFormRendererEngine());

        $this->assertEquals('Is active', $renderer->humanize('is_active'));
        $this->assertEquals('Is active', $renderer->humanize('isActive'));
    }

    public function testRenderARenderedField()
    {
        $this->expectException(BadMethodCallException::class);
        $this->expectExceptionMessage('Field "foo" has already been rendered, save the result of previous render call to a variable and output that instead.');

        $formView = new FormView();
        
__DIR__.'/Fixtures/templates/form',
        ]);

        $environment = new Environment($loader['strict_variables' => true]);
        $environment->addExtension(new TranslationExtension(new StubTranslator()));
        $environment->addExtension(new FormExtension());

        $rendererEngine = new TwigRendererEngine([
            'bootstrap_3_layout.html.twig',
            'custom_widgets.html.twig',
        ]$environment);
        $this->renderer = new FormRenderer($rendererEngine$this->createMock(CsrfTokenManagerInterface::class));
        $this->registerTwigRuntimeLoader($environment$this->renderer);
    }

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

        $html = $this->renderStart($form->createView());

        
$this->assertCount(0, $child1->getErrors()$child1->getName().' should not have an error, but has one');
        $this->assertCount(0, $child2->getErrors()$child2->getName().' should not have an error, but has one');
        $this->assertCount(0, $child3->getErrors()$child3->getName().' should not have an error, but has one');
        $this->assertCount(0, $child4->getErrors()$child4->getName().' should not have an error, but has one');
        $this->assertEquals([$this->getFormError($violation1$grandChild1)]iterator_to_array($grandChild1->getErrors())$grandChild1->getName().' should have an error, but has none');
        $this->assertEquals([$this->getFormError($violation2$grandChild2)]iterator_to_array($grandChild2->getErrors())$grandChild2->getName().' should have an error, but has none');
        $this->assertEquals([$this->getFormError($violation3$grandChild3)]iterator_to_array($grandChild3->getErrors())$grandChild3->getName().' should have an error, but has none');
    }

    public function testMessageWithLabel1()
    {
        $this->mapper = new ViolationMapper(new FormRenderer(new DummyFormRendererEngine())new FixedTranslator(['Name' => 'Custom Name']));

        $parent = $this->getForm('parent');
        $child = $this->getForm('name', 'name');
        $parent->add($child);

        $parent->submit([]);

        $violation = new ConstraintViolation('Message {{ label }}', null, [], null, 'data.name', null);
        $this->mapper->mapViolation($violation$parent);

        $this->assertCount(1, $child->getErrors()$child->getName().' should have an error, but has none');

        
__DIR__.'/Fixtures/templates/form',
        ]);

        $environment = new Environment($loader['strict_variables' => true]);
        $environment->addExtension(new TranslationExtension(new StubTranslator()));
        $environment->addExtension(new FormExtension());

        $rendererEngine = new TwigRendererEngine([
            'bootstrap_3_horizontal_layout.html.twig',
            'custom_widgets.html.twig',
        ]$environment);
        $this->renderer = new FormRenderer($rendererEngine$this->createMock(CsrfTokenManagerInterface::class));
        $this->registerTwigRuntimeLoader($environment$this->renderer);
    }

    protected function renderForm(FormView $view, array $vars = [])
    {
        return $this->renderer->renderBlock($view, 'form', $vars);
    }

    protected function renderLabel(FormView $view$label = null, array $vars = [])
    {
        if (null !== $label) {
            
$form = new NameExpression('form', 0);
        $resources = new ArrayExpression([
            new ConstantExpression(1, 0),
            new ConstantExpression('tpl1', 0),
            new ConstantExpression(0, 0),
            new ConstantExpression('tpl2', 0),
        ], 0);

        $node = new FormThemeNode($form$resources, 0);

        $environment = new Environment($this->createMock(LoaderInterface::class));
        $formRenderer = new FormRenderer($this->createMock(FormRendererEngineInterface::class));
        $this->registerTwigRuntimeLoader($environment$formRenderer);
        $compiler = new Compiler($environment);

        $this->assertEquals(
            sprintf(
                '$this->env->getRuntime("Symfony\\\\Component\\\\Form\\\\FormRenderer")->setTheme(%s, [1 => "tpl1", 0 => "tpl2"], true);',
                $this->getVariableGetter('form')
            ),
            trim($compiler->compile($node)->getSource())
        );

        


        $environment = new Environment($loader['strict_variables' => true]);
        $environment->addExtension(new TranslationExtension(new StubTranslator()));
        $environment->addGlobal('global', '');
        $environment->addExtension(new FormExtension());

        $rendererEngine = new TwigRendererEngine([
            'form_table_layout.html.twig',
            'custom_widgets.html.twig',
        ]$environment);
        $this->renderer = new FormRenderer($rendererEngine$this->createMock(CsrfTokenManagerInterface::class));
        $this->registerTwigRuntimeLoader($environment$this->renderer);
    }

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

        $html = $this->renderStart($form->createView());

        
Home | Imprint | This part of the site doesn't use cookies.