Skip to content

Commit bbcd5a4

Browse files
alexislefebvreAlexis Lefebvre
authored and
Alexis Lefebvre
committed
Explain submitWithAdditionalValues() usage and
how to remove existing data (a dynamically added field)
1 parent 8deb817 commit bbcd5a4

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

book/testing.rst

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,39 @@ their type::
687687
// Upload a file
688688
$form['photo']->upload('/path/to/lucas.jpg');
689689

690+
If you use a :doc:`Collection of Forms </cookbook/form/form_collections>`,
691+
you can't add fields to the existing form (this results in an error
692+
``Unreachable field "…"``). You can use the `submitWithAdditionalValues()`
693+
method in order to add new fields::
694+
695+
$crawler = $client->submitWithAdditionalValues(
696+
$form,
697+
array(),
698+
// New values:
699+
array('task[tags][0][name]' => 'tag1'),
700+
);
701+
702+
// The tag has been added.
703+
$this->assertEquals(1, $crawler->filter('ul.tags > li')->count());
704+
705+
Where ``task[tags][0][name]`` is the name of a field usually created
706+
with Javascript.
707+
708+
You can remove an existing field, e.g. a tag::
709+
710+
// Get the values of the form.
711+
$values = $form->getPhpValues();
712+
713+
// Remove the first tag.
714+
unset($values['task']['tags'][0]);
715+
716+
// Submit the data.
717+
$crawler = $client->request($form->getMethod(), $form->getUri(),
718+
$values, $form->getPhpFiles());
719+
720+
// The tag has been removed.
721+
$this->assertEquals(0, $crawler->filter('ul.tags > li')->count());
722+
690723
.. tip::
691724

692725
If you purposefully want to select "invalid" select/radio values, see

0 commit comments

Comments
 (0)