8000 feature #6427 [Testing] Explain how to add or remove data in a collec… · linaori/symfony-docs@0882fb5 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 0882fb5

Browse files
committed
feature symfony#6427 [Testing] Explain how to add or remove data in a collection of forms (alexislefebvre)
This PR was merged into the 2.3 branch. Discussion ---------- [Testing] Explain how to add or remove data in a collection of forms | Q | A | ------------- | --- | Doc fix? | no | New docs? | no | Applies to | all (based on 2.3) | Fixed tickets | - I added a part that explain how to test a [*Collection of Forms*](http://symfony.com/doc/2.3/cookbook/form/form_collections.html). Commits ------- 41276ff Tests: Explain how to add or remove data in a collection of forms
2 parents dc7cc73 + 41276ff commit 0882fb5

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

book/testing.rst

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,48 @@ their type::
699699
PHP format (it converts the keys with square brackets notation - e.g.
700700
``my_form[subject]`` - to PHP arrays).
701701

702+
If you use a :doc:`Collection of Forms </cookbook/form/form_collections>`,
703+
you can't add fields to an existing form with
704+
``$form['task[tags][0][name]'] = 'foo';``. This results in an error
705+
``Unreachable field "…"`` because ``$form`` can only be used in order to
706+
set values of existing fields. In order to add new fields, you have to
707+
add the values to the raw data array::
708+
709+
// Get the form.
710+
$form = $crawler->filter('button')->form();
711+
712+
// Get the raw values.
713+
$values = $form->getPhpValues();
714+
715+
// Add fields to the raw values.
716+
$values['task']['tag'][0]['name'] = 'foo';
717+
$values['task']['tag'][1]['name'] = 'bar';
718+
719+
// Submit the form with the existing and new values.
720+
$crawler = $this->client->request($form->getMethod(), $form->getUri(), $values,
721+
$form->getPhpFiles());
722+
723+
// The 2 tags have been added to the collection.
724+
$this->assertEquals(2, $crawler->filter('ul.tags > li')->count());
725+
726+
Where ``task[tags][0][name]`` is the name of a field created
727+
with JavaScript.
728+
729+
You can remove an existing field, e.g. a tag::
730+
731+
// Get the values of the form.
732+
$values = $form->getPhpValues();
733+
734+
// Remove the first tag.
735+
unset($values['task']['tags'][0]);
736+
737+
// Submit the data.
738+
$crawler = $client->request($form->getMethod(), $form->getUri(),
739+
$values, $form->getPhpFiles());
740+
741+
// The tag has been removed.
742+
$this->assertEquals(0, $crawler->filter('ul.tags > li')->count());
743+
702744
.. index::
703745
pair: Tests; Configuration
704746

0 commit comments

Comments
 (0)
0