Form 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;
        
private FormFactoryInterface $factory;

    protected function setUp(): void
    {
        $this->dispatcher = new EventDispatcher();
        $this->factory = (new FormFactoryBuilder())
            ->addExtension(new ValidatorExtension(Validation::createValidator()))
            ->getFormFactory();

        parent::setUp();

        $this->constraint = new Form();
    }

    public function testValidate()
    {
        $object = new \stdClass();
        $options = ['validation_groups' => ['group1', 'group2']];
        $form = $this->getCompoundForm($object$options);
        $form->submit([]);

        $this->expectValidateAt(0, 'data', $object['group1', 'group2']);

        


        $nodes = $dom->getElementsByTagName('input');

        try {
            new Form($nodes->item(0), 'http://example.com');
            $this->fail('__construct() throws a \\LogicException if the node has no form ancestor');
        } catch (\LogicException $e) {
            $this->assertTrue(true, '__construct() throws a \\LogicException if the node has no form ancestor');
        }

        try {
            new Form($nodes->item(1), 'http://example.com');
            $this->fail('__construct() throws a \\LogicException if the input type is not submit, button, or image');
        } catch (\LogicException $e) {
            $this->assertTrue(true, '__construct() throws a \\LogicException if the input type is not submit, button, or image');
        }

        
abstract protected function getFailedUploadedFile($errorCode);

    protected function createForm($name$method = null, $compound = false)
    {
        $config = $this->createBuilder($name$compound);

        if (null !== $method) {
            $config->setMethod($method);
        }

        return new Form($config);
    }

    protected function createBuilder($name$compound = false, array $options = [])
    {
        $builder = new FormBuilder($name, null, new EventDispatcher()new FormFactory(new FormRegistry([]new ResolvedFormTypeFactory()))$options);
        $builder->setCompound($compound);

        if ($compound) {
            $builder->setDataMapper(new DataMapper());
        }

        

        return true;
    }

    /** * Install config-form * * @return void */
    public function installForm()
    {
        $form = $this->Form();

        /** @var Form $parent */
        $parent = $this->Forms()->findOneBy(['name' => 'Core']);

        $form->setParent($parent);
        $form->setElement('textarea', 'cacheControllers', [
            'label' => 'Cache-Controller / Zeiten',
            'value' => "frontend/listing 3600\r\n" .
            "frontend/index 3600\r\n" .
            "frontend/detail 3600\r\n" .
            "frontend/campaign 14400\r\n" .
            
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()
    {
        $car = new \stdClass();
        $engine = new \stdClass();
        $engine->brand = 'Rolls-Royce';
        
if (strpos($config->controller, $request->getControllerName()) !== false) {
            $view->sCloudShow = true;
        }
    }

    /** * Translates the plugin form */
    private function addForm(): void
    {
        $form = $this->Form();
        $parent = $this->Forms()->findOneBy(['name' => 'Frontend']);
        $form->setParent($parent);
        $form->setElement('checkbox', 'show', [
            'label' => 'Tag-Cloud anzeigen',
            'value' => true,
            'scope' => Element::SCOPE_SHOP,
        ]);
        $form->setElement('text', 'controller', [
            'label' => 'Controller-Auswahl',
            'value' => 'index, listing',
            'scope' => Element::SCOPE_SHOP,
        ]);


    private function createForm($name = '', $compound = false)
    {
        $config = new FormBuilder($name, null, new EventDispatcher()(new FormFactoryBuilder())->getFormFactory());
        $config->setCompound($compound);

        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());
        
if ($groupedConstraints = self::getConstraintsInGroups($constraints$group)) {
                        $validator->atPath('data')->validate($data$groupedConstraints$group);
                    }

                    foreach ($form->all() as $field) {
                        if ($field->isSubmitted()) {
                            // remember to validate this field in one group only                             // otherwise resolving the groups would reuse the same                             // sequence recursively, thus some fields could fail                             // in different steps without breaking early enough                             $this->resolvedGroups[$field] = (array) $group;
                            $fieldFormConstraint = new Form();
                            $fieldFormConstraint->groups = $group;
                            $this->context->setNode($this->context->getValue()$field$this->context->getMetadata()$this->context->getPropertyPath());
                            $validator->atPath(sprintf('children[%s]', $field->getName()))->validate($field$fieldFormConstraint$group);
                        }
                    }

                    if ($violationsCount < $this->context->getViolations()->count()) {
                        break;
                    }
                }
            } else {
                
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 [
            [
/** * Register shopware auth resource * create pre-dispatch hook to check backend permissions */
    public function install()
    {
        $this->subscribeEvent('Enlight_Bootstrap_InitResource_Auth', 'onInitResourceAuth');
        $this->subscribeEvent('Enlight_Controller_Action_PreDispatch', 'onPreDispatchBackend');
        $this->subscribeEvent('Enlight_Bootstrap_InitResource_BackendSession', 'onInitResourceBackendSession');

        $form = $this->Form();
        $parent = $this->Forms()->findOneBy(['name' => 'Core']);
        $form->setParent($parent);
        $form->setLabel('Backend');
        $form->setElement('select', 'backendLocales', [
            'store' => 'base.Locale',
            'label' => 'Auswählbare Sprachen',
            'value' => [1, 2],
            'required' => true,
            'multiSelect' => true,
        ]);
        $form->setElement('interval', 'backendTimeout', [
            
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')];
        yield ['name', new PropertyPath('name')];
    }
$controller = $this->createController();

        $this->assertInstanceOf(NotFoundHttpException::class$controller->createNotFoundException());
    }

    public function testCreateForm()
    {
        $config = $this->createMock(FormConfigInterface::class);
        $config->method('getInheritData')->willReturn(false);
        $config->method('getName')->willReturn('');

        $form = new Form($config);

        $formFactory = $this->createMock(FormFactoryInterface::class);
        $formFactory->expects($this->once())->method('create')->willReturn($form);

        $container = new Container();
        $container->set('form.factory', $formFactory);

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

        $this->assertEquals($form$controller->createForm('foo'));
    }
$config->setCompound(true);
        $config->setDataMapper(new DataMapper());
        $config->setErrorBubbling($options['error_bubbling'] ?? false);

        if (!$synchronized) {
            $config->addViewTransformer(new CallbackTransformer(
                static fn ($normData) => $normData,
                static fn () => throw new TransformationFailedException()
            ));
        }

        return new Form($config);
    }

    protected function getConstraintViolation($propertyPath$root = null): ConstraintViolation
    {
        return new ConstraintViolation($this->message, $this->messageTemplate, $this->params, $root$propertyPath, null);
    }

    protected function getFormError(ConstraintViolationInterface $violation, FormInterface $form): FormError
    {
        $error = new FormError($this->message, $this->messageTemplate, $this->params, null, $violation);
        $error->setOrigin($form);

        
'onCollectJavascriptFiles'
        );

        $this->subscribeEvent(
            'Enlight_Controller_Action_PostDispatchSecure_Frontend',
            'onPostDispatch'
        );
    }

    private function createForm(): void
    {
        $form = $this->Form();

        $parent = $this->Forms()->findOneBy(['name' => 'Frontend']);
        $form->setParent($parent);

        $form->setElement('checkbox', 'show', [
            'label' => 'Menü anzeigen',
            'value' => 1,
            'scope' => Element::SCOPE_SHOP,
        ]);

        $form->setElement('number', 'hoverDelay', [
            
Home | Imprint | This part of the site doesn't use cookies.