FormBuilder example

$form->setData(null);
        $this->assertFalse($form->isEmpty());
    }

    private function createForm(): FormInterface
    {
        return $this->getBuilder()->getForm();
    }

    private function getBuilder(?string $name = 'name', string $dataClass = null, array $options = []): FormBuilder
    {
        return new FormBuilder($name$dataClassnew EventDispatcher()new FormFactory(new FormRegistry([]new ResolvedFormTypeFactory()))$options);
    }
}
'a' => 'bar',
                'b' => 'foo',
                'c' => 'baz',
                'id' => 'foo_bar',
                'name' => 'bar',
            ],
        ]$this->dataExtractor->extractViewVariables($view));
    }

    private function createBuilder(string $name, array $options = []): FormBuilder
    {
        return new FormBuilder($name, null, new EventDispatcher()new FormFactory(new FormRegistry([]new ResolvedFormTypeFactory()))$options);
    }
}
protected function setUp(): void
    {
        $this->factory = (new FormFactoryBuilder())->getFormFactory();
        $this->form = $this->getBuilder()
            ->setCompound(true)
            ->setDataMapper(new DataMapper())
            ->getForm();
    }

    protected function getBuilder($name = 'name')
    {
        return new FormBuilder($name, null, new EventDispatcher()$this->factory);
    }

    protected function getForm($name = 'name')
    {
        return $this->getBuilder($name)->getForm();
    }

    public function testPreSetDataResizesForm()
    {
        $this->form->add($this->getForm('0'));
        $this->form->add($this->getForm('1'));

        
$builder
                ->setCompound(true)
                ->setDataMapper(new DataMapper())
            ;
        }

        return $builder->getForm();
    }

    private function getBuilder(string $name = 'name', string $dataClass = null, array $options = []): FormBuilder
    {
        return new FormBuilder($name$dataClassnew EventDispatcher()$this->factory, $options);
    }
}
false, false, false],
        ];
    }

    private function getButtonBuilder($name)
    {
        return new ButtonBuilder($name);
    }

    private function getFormBuilder()
    {
        return new FormBuilder('form', null, new EventDispatcher()new FormFactory(new FormRegistry([]new ResolvedFormTypeFactory())));
    }
}

    protected function newBuilder(string $name, ?string $dataClass, FormFactoryInterface $factory, array $options): FormBuilderInterface
    {
        if ($this->innerType instanceof ButtonTypeInterface) {
            return new ButtonBuilder($name$options);
        }

        if ($this->innerType instanceof SubmitButtonTypeInterface) {
            return new SubmitButtonBuilder($name$options);
        }

        return new FormBuilder($name$dataClassnew EventDispatcher()$factory$options);
    }

    /** * Creates a new view instance. * * Override this method if you want to customize the view class. */
    protected function newView(FormView $parent = null): FormView
    {
        return new FormView($parent);
    }
}

        return new FormValidator();
    }

    private function getBuilder(string $name = 'name', string $dataClass = null, array $options = []): FormBuilder
    {
        $options = array_replace([
            'constraints' => [],
            'invalid_message_parameters' => [],
        ]$options);

        return new FormBuilder($name$dataClass$this->dispatcher, $this->factory, $options);
    }

    private function getForm($name = 'name', $dataClass = null, array $options = [])
    {
        return $this->getBuilder($name$dataClass$options)->getForm();
    }

    private function getCompoundForm($data, array $options = [])
    {
        return $this->getBuilder('name', \is_object($data) ? $data::class D null, $options)
            ->setData($data)
            
private MockObject&FormFactoryInterface $factory;

    protected function setUp(): void
    {
        $this->collection = new ArrayCollection(['test']);
        $this->dispatcher = new EventDispatcher();
        $this->factory = $this->createMock(FormFactoryInterface::class);
    }

    protected function getBuilder()
    {
        return new FormBuilder('name', null, $this->dispatcher, $this->factory);
    }

    protected function getForm()
    {
        return $this->getBuilder()
            ->setData($this->collection)
            ->addEventSubscriber(new MergeDoctrineCollectionListener())
            ->getForm();
    }

    public function testOnSubmitDoNothing()
    {
use Symfony\Component\Form\Tests\Fixtures\CustomArrayObject;

class MergeCollectionListenerCustomArrayObjectTest extends MergeCollectionListenerTestCase
{
    protected function getData(array $data)
    {
        return new CustomArrayObject($data);
    }

    protected function getBuilder($name = 'name'): FormBuilderInterface
    {
        return new FormBuilder($name, 'Symfony\Component\Form\Tests\Fixtures\CustomArrayObject', new EventDispatcher()(new FormFactoryBuilder())->getFormFactory());
    }
}


    public function testBuildForm()
    {
        $eventDispatcher = new EventDispatcher();
        $this->assertFalse($eventDispatcher->hasListeners(FormEvents::PRE_SET_DATA));
        $this->assertFalse($eventDispatcher->hasListeners(FormEvents::POST_SET_DATA));
        $this->assertFalse($eventDispatcher->hasListeners(FormEvents::PRE_SUBMIT));
        $this->assertFalse($eventDispatcher->hasListeners(FormEvents::SUBMIT));
        $this->assertFalse($eventDispatcher->hasListeners(FormEvents::POST_SUBMIT));

        $this->extension->buildForm(new FormBuilder(null, null, $eventDispatchernew FormFactory(new FormRegistry([]new ResolvedFormTypeFactory())))[]);

        $this->assertFalse($eventDispatcher->hasListeners(FormEvents::PRE_SET_DATA));
        $this->assertTrue($eventDispatcher->hasListeners(FormEvents::POST_SET_DATA));
        $this->assertFalse($eventDispatcher->hasListeners(FormEvents::PRE_SUBMIT));
        $this->assertFalse($eventDispatcher->hasListeners(FormEvents::SUBMIT));
        $this->assertTrue($eventDispatcher->hasListeners(FormEvents::POST_SUBMIT));
    }
}

        $this->dispatcher = new EventDispatcher();
        $this->factory = (new FormFactoryBuilder())->getFormFactory();
        $this->tokenManager = new CsrfTokenManager();
        $this->form = $this->getBuilder()
            ->setDataMapper(new DataMapper())
            ->getForm();
    }

    protected function getBuilder()
    {
        return new FormBuilder('post', null, $this->dispatcher, $this->factory, ['compound' => true]);
    }

    // https://github.com/symfony/symfony/pull/5838     public function testStringFormData()
    {
        $data = 'XP4HUzmHPi';
        $event = new FormEvent($this->form, $data);

        $validation = new CsrfValidationListener('csrf', $this->tokenManager, 'unknown', 'Invalid.');
        $validation->preSubmit($event);

        
$this->requestStack = new RequestStack();
    $this->requestStack->push($this->request);
    $this->logger = $this->createMock('Drupal\Core\Logger\LoggerChannelInterface');
    $form_error_handler = $this->createMock('Drupal\Core\Form\FormErrorHandlerInterface');
    $this->formValidator = new FormValidator($this->requestStack, $this->getStringTranslationStub()$this->csrfToken, $this->logger, $form_error_handler);
    $this->formSubmitter = $this->getMockBuilder('Drupal\Core\Form\FormSubmitter')
      ->setConstructorArgs([$this->requestStack, $this->urlGenerator])
      ->onlyMethods(['batchGet'])
      ->getMock();
    $this->root = dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__)), 2);

    $this->formBuilder = new FormBuilder($this->formValidator, $this->formSubmitter, $this->formCache, $this->moduleHandler, $this->eventDispatcher, $this->requestStack, $this->classResolver, $this->elementInfo, $this->themeManager, $this->csrfToken);
  }

  /** * {@inheritdoc} */
  protected function tearDown(): void {
    Html::resetSeenIds();
    (new FormState())->clearErrors();
  }

  /** * Provides a mocked form object. * * @param string $form_id * The form ID to be used. * @param mixed $expected_form * (optional) If provided, the expected form response for buildForm() to * return. Defaults to NULL. * @param int $count * (optional) The number of times the form is expected to be built. Defaults * to 1. * * @return \PHPUnit\Framework\MockObject\MockObject|\Drupal\Core\Form\FormInterface * The mocked form object. */
use Symfony\Component\Form\FormFactoryBuilder;

class MergeCollectionListenerArrayTest extends MergeCollectionListenerTestCase
{
    protected function getData(array $data)
    {
        return $data;
    }

    protected function getBuilder($name = 'name'): FormBuilderInterface
    {
        return new FormBuilder($name, null, new EventDispatcher()(new FormFactoryBuilder())->getFormFactory());
    }
}
use Symfony\Component\Form\ResolvedFormTypeFactory;
use Symfony\Component\Form\SubmitButtonBuilder;

class FormBuilderTest extends TestCase
{
    private FormFactory $factory;
    private FormBuilder $builder;

    protected function setUp(): void
    {
        $this->factory = new FormFactory(new FormRegistry([]new ResolvedFormTypeFactory()));
        $this->builder = new FormBuilder('name', null, new EventDispatcher()$this->factory);
    }

    /** * Changing the name is not allowed, otherwise the name and property path * are not synchronized anymore. * * @see FormType::buildForm() */
    public function testNoSetName()
    {
        $this->assertFalse(method_exists($this->builder, 'setName'));
    }
use Symfony\Component\Form\FormFactoryBuilder;

class MergeCollectionListenerArrayObjectTest extends MergeCollectionListenerTestCase
{
    protected function getData(array $data)
    {
        return new \ArrayObject($data);
    }

    protected function getBuilder($name = 'name'): FormBuilderInterface
    {
        return new FormBuilder($name, \ArrayObject::classnew EventDispatcher()(new FormFactoryBuilder())->getFormFactory());
    }
}
Home | Imprint | This part of the site doesn't use cookies.