FormEvent example


    public function testAddExtraEntriesIfAllowAdd($allowDelete)
    {
        $originalData = $this->getData([1 => 'second']);
        $newData = $this->getData([0 => 'first', 1 => 'second', 2 => 'third']);

        $listener = new MergeCollectionListener(true, $allowDelete);

        $this->form->setData($originalData);

        $event = new FormEvent($this->form, $newData);
        $listener->onSubmit($event);

        // The original object was modified         if (\is_object($originalData)) {
            $this->assertSame($originalData$event->getData());
        }

        // The original object matches the new object         $this->assertEquals($newData$event->getData());
    }

    
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;
        $form = new Form(new FormConfigBuilder('name', null, new EventDispatcher()));
        
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 [
            ['www.symfony.com'],
            [
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'));

        $data = [1 => 'string', 2 => 'string'];
        $event = new FormEvent($this->form, $data);
        $listener = new ResizeFormListener(TextType::class['attr' => ['maxlength' => 10]], false, false);
        $listener->preSetData($event);

        $this->assertFalse($this->form->has('0'));
        $this->assertTrue($this->form->has('1'));
        $this->assertTrue($this->form->has('2'));
    }

    public function testPreSetDataRequiresArrayOrTraversable()
    {
        $this->expectException(UnexpectedTypeException::class);
        


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

        // Validate accordingly         $this->assertSame($data$event->getData());
    }

    public function testArrayCsrfToken()
    {
        $event = new FormEvent($this->form, ['csrf' => []]);

        


    // 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()
    {
        $violation = new ConstraintViolation($this->message, $this->messageTemplate, $this->params, null, 'data', null, null, FormConstraint::NOT_SYNCHRONIZED_ERROR, new FormConstraint());
        $form = new SubmittedNotSynchronizedForm(new FormConfigBuilder('street', null, new EventDispatcher()));

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

    public function testOnSubmitDoNothing()
    {
        $submittedData = ['test'];
        $event = new FormEvent($this->getForm()$submittedData);

        $this->dispatcher->dispatch($event, FormEvents::SUBMIT);

        $this->assertTrue($this->collection->contains('test'));
        $this->assertSame(1, $this->collection->count());
    }

    public function testOnSubmitNullClearCollection()
    {
        $submittedData = [];
        $event = new FormEvent($this->getForm()$submittedData);

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