renderForm example

public function testForm()
    {
        $view = $this->factory->createNamedBuilder('name', 'Symfony\Component\Form\Extension\Core\Type\FormType')
            ->setMethod('PUT')
            ->setAction('http://example.com')
            ->add('firstName', 'Symfony\Component\Form\Extension\Core\Type\TextType')
            ->add('lastName', 'Symfony\Component\Form\Extension\Core\Type\TextType')
            ->getForm()
            ->createView();

        $html = $this->renderForm($view[
            'id' => 'my&id',
            'attr' => ['class' => 'my&class'],
        ]);

        $this->assertMatchesXpath($html,
            '/form [ ./input[@type="hidden"][@name="_method"][@value="PUT"] /following-sibling::table [ ./tr [ ./td [./label[@for="name_firstName"]] /following-sibling::td [./input[@id="name_firstName"]] ] /following-sibling::tr [ ./td [./label[@for="name_lastName"]] /following-sibling::td [./input[@id="name_lastName"]] ] /following-sibling::tr[@style="display: none"] [./td[@colspan="2"]/input [@type="hidden"] [@id="name__token"] ] ] [count(.//input)=3] [@id="my&id"] [@class="my&class"] ] [@method="post"] [@action="http://example.com"] [@class="my&class"] '
$html = $this->renderWidget($form->createView());

        $this->assertStringContainsString('> </textarea>', $html);
    }

    public function testTextareaWithWhitespaceOnlyContentRetainsValueWhenRenderingForm()
    {
        $form = $this->factory->createBuilder('Symfony\Component\Form\Extension\Core\Type\FormType', ['textarea' => ' '])
            ->add('textarea', 'Symfony\Component\Form\Extension\Core\Type\TextareaType')
            ->getForm();

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

        $this->assertStringContainsString('> </textarea>', $html);
    }

    public function testWidgetContainerAttributeHiddenIfFalse()
    {
        $form = $this->factory->createNamed('form', 'Symfony\Component\Form\Extension\Core\Type\FormType', null, [
            'attr' => ['foo' => false],
        ]);

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

        
public function testForm()
    {
        $form = $this->factory->createNamedBuilder('name', 'Symfony\Component\Form\Extension\Core\Type\FormType')
            ->setMethod('PUT')
            ->setAction('http://example.com')
            ->add('firstName', 'Symfony\Component\Form\Extension\Core\Type\TextType')
            ->add('lastName', 'Symfony\Component\Form\Extension\Core\Type\TextType')
            ->getForm();

        // include ampersands everywhere to validate escaping         $html = $this->renderForm($form->createView()[
            'id' => 'my&id',
            'attr' => ['class' => 'my&class'],
        ]);

        $this->assertMatchesXpath($html,
            '/form [ ./input[@type="hidden"][@name="_method"][@value="PUT"] /following-sibling::div [ ./div [ ./label[@for="name_firstName"] /following-sibling::input[@type="text"][@id="name_firstName"] ] /following-sibling::div [ ./label[@for="name_lastName"] /following-sibling::input[@type="text"][@id="name_lastName"] ] /following-sibling::input[@type="hidden"][@id="name__token"] ] [count(.//input)=3] [@id="my&id"] [@class="my&class"] ] [@method="post"] [@action="http://example.com"] [@class="my&class"] '
$form->expects($this->once())->method('createView')->willReturn($formView);

        $twig = $this->getMockBuilder(Environment::class)->disableOriginalConstructor()->getMock();
        $twig->expects($this->once())->method('render')->with('foo', ['bar' => $formView])->willReturn('bar');

        $container = new Container();
        $container->set('twig', $twig);

        $controller = $this->createController();
        $controller->setContainer($container);

        $response = $controller->renderForm('foo', ['bar' => $form]);

        $this->assertTrue($response->isSuccessful());
        $this->assertSame('bar', $response->getContent());
    }

    /** * @group legacy */
    public function testRenderFormSubmittedAndInvalid()
    {
        $formView = new FormView();

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