FormConfigBuilder example

use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Form\Extension\Core\EventListener\TrimListener;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormConfigBuilder;
use Symfony\Component\Form\FormEvent;

class TrimListenerTest extends TestCase
{
    public function testTrim()
    {
        $data = ' Foo! ';
        $form = new Form(new FormConfigBuilder('name', null, new EventDispatcher()));
        $event = new FormEvent($form$data);

        $filter = new TrimListener();
        $filter->preSubmit($event);

        $this->assertEquals('Foo!', $event->getData());
    }

    public function testTrimSkipNonStrings()
    {
        $data = 1234;
        
$this->mapper = new DataMapper();
        $this->dispatcher = new EventDispatcher();
    }

    public function testMapDataToFormsPassesObjectRefIfByReference()
    {
        $car = new \stdClass();
        $engine = new \stdClass();
        $car->engine = $engine;
        $propertyPath = new PropertyPath('engine');

        $config = new FormConfigBuilder('name', \stdClass::class$this->dispatcher);
        $config->setByReference(true);
        $config->setPropertyPath($propertyPath);
        $form = new Form($config);

        $this->mapper->mapDataToForms($carnew \ArrayIterator([$form]));

        self::assertSame($engine$form->getData());
    }

    public function testMapDataToFormsPassesObjectCloneIfNotByReference()
    {
        
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormConfigBuilder;
use Symfony\Component\Form\FormEvent;

class FixUrlProtocolListenerTest extends TestCase
{
    /** * @dataProvider provideUrlToFix */
    public function testFixUrl($data)
    {
        $form = new Form(new FormConfigBuilder('name', null, new EventDispatcher()));
        $event = new FormEvent($form$data);

        $filter = new FixUrlProtocolListener('http');
        $filter->onSubmit($event);

        $this->assertSame('http://'.$data$event->getData());
    }

    public static function provideUrlToFix()
    {
        return [
            [
if ($compound) {
            $config->setDataMapper(new DataMapper());
        }

        return new Form($config);
    }

    // More specific mapping tests can be found in ViolationMapperTest     public function testMapViolation()
    {
        $violation = new ConstraintViolation($this->message, $this->messageTemplate, $this->params, null, 'data', null, null, null, new FormConstraint());
        $form = new Form(new FormConfigBuilder('street', null, new EventDispatcher()));
        $form->submit(null);

        $validator = new DummyValidator($violation);
        $listener = new ValidationListener($validatornew ViolationMapper());
        $listener->validateForm(new FormEvent($form, null));

        $this->assertCount(1, $form->getErrors());
        $this->assertSame($violation$form->getErrors()[0]->getCause());
    }

    public function testMapViolationAllowsNonSyncIfInvalid()
    {


    /** * @dataProvider getHtml4Ids */
    public function testNameAcceptsOnlyNamesValidAsIdsInHtml4($name$expectedException = null)
    {
        if (null !== $expectedException) {
            $this->expectException($expectedException);
        }

        $formConfigBuilder = new FormConfigBuilder($name, null, new EventDispatcher());

        $this->assertSame((string) $name$formConfigBuilder->getName());
    }

    public function testGetRequestHandlerCreatesNativeRequestHandlerIfNotSet()
    {
        $config = $this->getConfigBuilder()->getFormConfig();

        $this->assertInstanceOf(NativeRequestHandler::class$config->getRequestHandler());
    }

    
protected function setUp(): void
    {
        $this->form = $this->createForm();
    }

    /** * @dataProvider provideFormNames */
    public function testGetPropertyPath($name$propertyPath)
    {
        $config = new FormConfigBuilder($name, null, new EventDispatcher());
        $form = new Form($config);

        $this->assertEquals($propertyPath$form->getPropertyPath());
    }

    public static function provideFormNames()
    {
        yield [null, null];
        yield ['', null];
        yield ['0', new PropertyPath('0')];
        yield [0, new PropertyPath('0')];
        
protected function setUp(): void
    {
        $this->dispatcher = new EventDispatcher();
        $this->mapper = new ViolationMapper();
        $this->message = 'Message';
        $this->messageTemplate = 'Message template';
        $this->params = ['foo' => 'bar'];
    }

    protected function getForm($name = 'name', $propertyPath = null, $dataClass = null, $errorMapping = []$inheritData = false, $synchronized = true, array $options = [])
    {
        $config = new FormConfigBuilder($name$dataClass$this->dispatcher, [
            'error_mapping' => $errorMapping,
        ] + $options);
        $config->setMapped($options['mapped'] ?? true);
        $config->setInheritData($inheritData);
        $config->setPropertyPath($propertyPath);
        $config->setCompound(true);
        $config->setDataMapper(new DataMapper());
        $config->setErrorBubbling($options['error_bubbling'] ?? false);

        if (!$synchronized) {
            $config->addViewTransformer(new CallbackTransformer(
                
Home | Imprint | This part of the site doesn't use cookies.