Functional Testing Collection Form Fields #4124

Open
gzankevich opened this Issue Apr 26, 2012 · 20 comments

Projects

None yet
@gzankevich

With form collections, we use Javascript to add the new fields and Symfony handles the rest.

In the following example, Office has a many-to-many with Contact.

When attempting the same approach in functional tests, e.g. by doing:

    $form = $buttonCrawlerNode->form(array(
            'company_whateverbundle_officetype[office_contacts][0][contact]' => '1'
    ));

The actual result is:

InvalidArgumentException: The form field "company_whateverbundle_officetype[office_contacts][0][contact]" does not exist.

The expected result is that a new contact is added.

Steps to reproduce:

  1. Create a couple of entities with a many-to-many association between them.
  2. Create a form with a collection field to edit the associations.
  3. Create a functional test and try to create a new association with it.
@gzankevich

Same deal with collections of "file" entities when calling:

$form['company_whateverbundle_officetype[image_attachment][0][file]']->upload(__DIR__.'/../../Fixtures/SampleImage.png');
@stof
Member
stof commented Apr 26, 2012

Are you setting the allow_add option to true for the CollectionType ?

@gzankevich

Yes, it works fine in dev/prod environments.

@leftdevel

Hi @gzankevich, did you managed it?

@gzankevich

Issue still persists. I'll try it out on a clean installation and see what happens.

@r4cker
r4cker commented Jul 24, 2012

I am facing the same problem.

  • Works fine in dev/prod
  • allow_add set to TRUE

@gzankevich Did you find what happend?

@gzankevich

I've created a Gist containing all of the code necessary to reproduce the problem: https://gist.github.com/3193910

@carlescliment

Subscribing.

I'll try submitting the form with a $client->request('POST', $data).

Not optimal, but it should at least let me test the controller.

@sstok
Contributor
sstok commented Feb 7, 2013

I was facing this problem to today, I actually needed to remove a row from the collection 😄

But found I little trick to make this work.

$form = $crawler->selectButton('save')->form();
$form->setValues(array(
    'customer_invoice_form[customer]' => 'C0001',
    'customer_invoice_form[address]'  => "274 Rose avenue US\n212-35 OKMontgomery",

    'customer_invoice_form[items][0][number]'        => 1,
    'customer_invoice_form[items][0][label]'         => 'Domainreg bar-foo.com',
    'customer_invoice_form[items][0][period][start]' => '01-01-2013',
    'customer_invoice_form[items][0][period][end]'   => '01-12-2013',
    'customer_invoice_form[items][0][price]'         => '20,00',
    'customer_invoice_form[items][0][taxPercent]'    => '21,10',
));

// Now do what ever you want with the $values.
$values = $form->getPhpValues();
unset($values['customer_invoice_form']['items'][1]);

$client->request($form->getMethod(), $form->getUri(), $values, $form->getPhpFiles());
//$client->submit($form);
@ste93cry

I have the same problem: I'm using knockout to render a form client-side with javascript instead of rendering it with twig, however I can't submit any form which has inputs that don't exist. The problem is clear: the inputs are rendered client-side, so when I try to use the crawler to get them and change their values obviously it can't find them. There should be a way in the Symfony testing framework to submit data that isn't part of the form

@sstok
Contributor
sstok commented Mar 15, 2013

@ste93cry Try my method ;)

@leftdevel

+1 @sstok. That is the way to go in phpunit.

@fnagel
fnagel commented Feb 20, 2014

@sstok Is your workaround supposed to work in Symfony 2.3.x?

@fnagel
fnagel commented Feb 20, 2014

Ok, here's another workaround:

$form = $crawler->filter('form .submit-button .btn-primary')->form();
$formData = array(
    'step2Type[positions][0][property1]' => '1',
    'step2Type[positions][0][property2]' => 'xyz',
);

$this->client->request($form->getMethod(), $form->getUri(), $form->getPhpValues(), $form->getPhpFiles(), array(), http_build_query($formData))
@gagarine

I have field than as formed with the name like
properties[0].value
properties[1].value

And no workarounds works.
I can't change the field name has it's on a service I don't control...

@wouterj
Member
wouterj commented Aug 11, 2014

This seems more like a mailinglist item than a bug/feature report afaics.

@jakzal
Member
jakzal commented May 12, 2015

re #3824

@placid2000

I've faced the same a few times now and ignored it ill now.. Still no solution there? The workarounds don't seem to work for me neither

@sstok
Contributor
sstok commented Jul 23, 2015
properties[0].value
properties[1].value

Are property-paths, a property-path cannot be used for the DOMCrawler.

fnagel's solution works as it doesn't actually use the DOMCrawler but creates the request directly instead.

@webmozart webmozart removed the Form label Mar 9, 2016
@webmozart webmozart changed the title from [Form] Functional Testing Collection Form Fields to Functional Testing Collection Form Fields Mar 9, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment