createNamed example



    /** * @dataProvider getPostMaxSizeFixtures */
    public function testAddFormErrorIfPostMaxSizeExceeded(?int $contentLength, string $iniMax, bool $shouldFail, array $errorParams = [])
    {
        $this->serverParams->contentLength = $contentLength;
        $this->serverParams->postMaxSize = $iniMax;

        $options = ['post_max_size_message' => 'Max {{ max }}!'];
        $form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, $options);
        $this->setRequestData('POST', [][]);

        $this->requestHandler->handleRequest($form$this->request);

        if ($shouldFail) {
            $error = new FormError($options['post_max_size_message'], null, $errorParams);
            $error->setOrigin($form);

            $this->assertEquals([$error]iterator_to_array($form->getErrors()));
            $this->assertTrue($form->isSubmitted());
        } else {
            
use Symfony\Component\Form\FormError;

/** * Abstract class providing test cases for the Bootstrap 5 Twig form theme. * * @author Romain Monteil <monteil.romain@gmail.com> */
abstract class AbstractBootstrap5LayoutTestCase extends AbstractBootstrap4LayoutTestCase
{
    public function testRow()
    {
        $form = $this->factory->createNamed('')->add('name', TextType::class);
        $form->get('name')->addError(new FormError('[trans]Error![/trans]'));
        $html = $this->renderRow($form->get('name')->createView());

        $this->assertMatchesXpath($html,
            '/div [@class="mb-3"] [ ./label[@for="name"] /following-sibling::input[@id="name"] /following-sibling::div [@class="invalid-feedback d-block"] [.="[trans]Error![/trans]"] ] [count(./div)=1] '
$environment->addExtension(new FormExtension());
        $environment->setCharset('ISO-8859-1');

        $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);

        $view = $this->factory
            ->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\MoneyType')
            ->createView()
        ;

        $this->assertSame(<<<'HTML' <div class="input-group"> <span class="input-group-addon">&euro; </span> <input type="text" id="name" name="name" required="required" class="form-control"> </div> HTML
            , trim($this->renderWidget($view)));
    }

    
$form->submit(['first_changed', 'second']);

        $this->assertTrue($form->has('0'));
        $this->assertTrue($form->has('1'));
        $this->assertSame('first', $form[0]->getData());
        $this->assertSame('second', $form[1]->getData());
        $this->assertSame(['first', 'second']$form->getData());
    }

    public function testEntriesBlockPrefixes()
    {
        $collectionView = $this->factory->createNamed('fields', static::TESTED_TYPE, [''][
            'allow_add' => true,
        ])
            ->createView()
        ;

        $expectedBlockPrefixes = [
            'form',
            'collection_entry',
            'text',
            '_fields_entry',
        ];

        
$environment->addExtension(new FormExtension());
        $environment->setCharset('ISO-8859-1');

        $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);

        $view = $this->factory
            ->createNamed('name', MoneyType::class)
            ->createView();

        self::assertSame(<<<'HTML' <div class="input-group "><span class="input-group-text">&euro; </span><input type="text" id="name" name="name" required="required" class="form-control"></div> HTML
            , trim($this->renderWidget($view)));
    }

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

        $this->assertMatchesXpath(
            $this->renderWidget($view),
            '/input[@type="email"][@rel="theme"]'
        );
    }

    
// Never initialize child forms automatically             $options['auto_initialize'] = false;

            if (null === $type && null === $this->config->getDataClass()) {
                $type = TextType::class;
            }

            if (null === $type) {
                $child = $this->config->getFormFactory()->createForProperty($this->config->getDataClass()$child, null, $options);
            } else {
                $child = $this->config->getFormFactory()->createNamed($child$type, null, $options);
            }
        } elseif ($child->getConfig()->getAutoInitialize()) {
            throw new RuntimeException(sprintf('Automatic initialization is only supported on root forms. You should set the "auto_initialize" option to false on the field "%s".', $child->getName()));
        }

        $this->children[$child->getName()] = $child;

        $child->setParent($this);

        // If setData() is currently being called, there is no need to call         // mapDataToForms() here, as mapDataToForms() is called at the end
abstract protected function renderRest(FormView $view, array $vars = []);

    abstract protected function renderStart(FormView $view, array $vars = []);

    abstract protected function renderEnd(FormView $view, array $vars = []);

    abstract protected function setTheme(FormView $view, array $themes$useDefaultThemes = true);

    public function testLabel()
    {
        $form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType');
        $view = $form->createView();
        $this->renderWidget($view['label' => 'foo']);
        $html = $this->renderLabel($view);

        $this->assertMatchesXpath($html,
            '/label [@for="name"] [.="[trans]Name[/trans]"] '
        );
    }

    
use Symfony\Component\OptionsResolver\Exception\MissingOptionsException;
use Symfony\Component\Translation\IdentityTranslator;
use Symfony\Contracts\Translation\TranslatableInterface;

class EnumTypeTest extends BaseTypeTestCase
{
    public const TESTED_TYPE = EnumType::class;

    public function testClassOptionIsRequired()
    {
        $this->expectException(MissingOptionsException::class);
        $this->factory->createNamed('name', $this->getTestedType());
    }

    public function testInvalidClassOption()
    {
        $this->expectException(InvalidOptionsException::class);
        $this->factory->createNamed('name', $this->getTestedType(), null, [
            'class' => 'foo',
        ]);
    }

    public function testInvalidClassOptionType()
    {


namespace Symfony\Bridge\Twig\Tests\Extension;

use Symfony\Component\Form\FormError;
use Symfony\Component\Security\Csrf\CsrfToken;

abstract class AbstractDivLayoutTestCase extends AbstractLayoutTestCase
{
    public function testRow()
    {
        $form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType');
        $form->addError(new FormError('[trans]Error![/trans]'));
        $view = $form->createView();
        $html = $this->renderRow($view);

        $this->assertMatchesXpath($html,
            '/div [ ./label[@for="name"] /following-sibling::ul [./li[.="[trans]Error![/trans]"]] [count(./li)=1] /following-sibling::input[@id="name"] ] '


namespace Symfony\Bridge\Twig\Tests\Extension;

use Symfony\Component\Form\Extension\Core\Type\PercentType;
use Symfony\Component\Form\FormError;

abstract class AbstractBootstrap3LayoutTestCase extends AbstractLayoutTestCase
{
    public function testLabelOnForm()
    {
        $form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\DateType', null, ['widget' => 'choice']);
        $view = $form->createView();
        $this->renderWidget($view['label' => 'foo']);
        $html = $this->renderLabel($view);

        $this->assertMatchesXpath($html,
            '/label [@class="control-label required"] [.="[trans]Name[/trans]"] '
        );
    }

    

    public function finishView(FormView $view, FormInterface $form, array $options)
    {
        if ($options['csrf_protection'] && !$view->parent && $options['compound']) {
            $factory = $form->getConfig()->getFormFactory();
            $tokenId = $options['csrf_token_id'] ?: ($form->getName() ?: $form->getConfig()->getType()->getInnerType()::class);
            $data = (string) $options['csrf_token_manager']->getToken($tokenId);

            $csrfForm = $factory->createNamed($options['csrf_field_name'], HiddenType::class$data[
                'block_prefix' => 'csrf_token',
                'mapped' => false,
            ]);

            $view->children[$options['csrf_field_name']] = $csrfForm->createView($view);
        }
    }

    /** * @return void */
    
$this->form->submit([], false);

        $this->assertFalse($child->isSubmitted());
    }

    public function testSubmitDoesNotAddExtraFieldForNullValues()
    {
        $factory = Forms::createFormFactoryBuilder()
            ->getFormFactory();

        $child = $factory->createNamed('file', 'Symfony\Component\Form\Extension\Core\Type\FileType', null, ['auto_initialize' => false]);

        $this->form->add($child);
        $this->form->submit(['file' => null], false);

        $this->assertCount(0, $this->form->getExtraData());
    }

    public function testClearMissingFlagIsForwarded()
    {
        $personForm = $this->createForm('person');

        
$this->em->persist($entity);
        }

        $this->em->flush();
        // no clear, because entities managed by the choice field must         // be managed!     }

    public function testClassOptionIsRequired()
    {
        $this->expectException(MissingOptionsException::class);
        $this->factory->createNamed('name', static::TESTED_TYPE);
    }

    public function testInvalidClassOption()
    {
        $this->expectException(RuntimeException::class);
        $this->factory->createNamed('name', static::TESTED_TYPE, null, [
            'class' => 'foo',
        ]);
    }

    /** * @dataProvider choiceTranslationDomainProvider */
$this->assertEquals('token', $view['csrf']->vars['value']);
    }

    public function testGenerateCsrfTokenUsesFormNameAsIntentionByDefault()
    {
        $this->tokenManager->expects($this->once())
            ->method('getToken')
            ->with('FORM_NAME')
            ->willReturn(new CsrfToken('TOKEN_ID', 'token'));

        $view = $this->factory
            ->createNamed('FORM_NAME', 'Symfony\Component\Form\Extension\Core\Type\FormType', null, [
                'csrf_field_name' => 'csrf',
                'csrf_token_manager' => $this->tokenManager,
                'compound' => true,
            ])
            ->createView();

        $this->assertEquals('token', $view['csrf']->vars['value']);
    }

    public function testGenerateCsrfTokenUsesTypeClassAsIntentionIfEmptyFormName()
    {
        
Home | Imprint | This part of the site doesn't use cookies.