diff --git a/book/controller.rst b/book/controller.rst index 71e8c88b69c..22a07737648 100644 --- a/book/controller.rst +++ b/book/controller.rst @@ -260,7 +260,7 @@ to ``$name``. Just make sure they the name of the placeholder is the same as the name of the argument variable. Take the following more-interesting example, where the controller has two -arguments:: +arguments: .. configuration-block:: @@ -704,7 +704,7 @@ The Request and Response Object ------------------------------- As mentioned :ref:`earlier `, the framework will -pass the ``Request`` object to any controller argument taht is type-hinted with +pass the ``Request`` object to any controller argument that is type-hinted with the ``Request`` class:: use Symfony\Component\HttpFoundation\Request; diff --git a/book/testing.rst b/book/testing.rst index 5d088e7345a..bda2ede4ef8 100644 --- a/book/testing.rst +++ b/book/testing.rst @@ -699,6 +699,48 @@ their type:: PHP format (it converts the keys with square brackets notation - e.g. ``my_form[subject]`` - to PHP arrays). +If you use a :doc:`Collection of Forms `, +you can't add fields to an existing form with +``$form['task[tags][0][name]'] = 'foo';``, this results in an error +``Unreachable field "…"`` because ``$form`` can only be used in order to +set values of existing fields. In order to add new fields, you have to +add the values to the raw data array:: + + // Get the form. + $form = $crawler->filter('button')->form(); + + // Get the raw values. + $values = $form->getPhpValues(); + + // Add fields to the raw values. + $values['task']['tag'][0]['name'] = 'foo'; + $values['task']['tag'][1]['name'] = 'bar'; + + // Submit the form with the existing and new values. + $crawler = $this->client->request($form->getMethod(), $form->getUri(), $values, + $form->getPhpFiles()); + + // The tag has been added to the collection. + $this->assertEquals(2, $crawler->filter('ul.tags > li')->count()); + +Where ``task[tags][0][name]`` is the name of a field created +with Javascript. + +You can remove an existing field, e.g. a tag:: + + // Get the values of the form. + $values = $form->getPhpValues(); + + // Remove the first tag. + unset($values['task']['tags'][0]); + + // Submit the data. + $crawler = $client->request($form->getMethod(), $form->getUri(), + $values, $form->getPhpFiles()); + + // The tag has been removed. + $this->assertEquals(0, $crawler->filter('ul.tags > li')->count()); + .. index:: pair: Tests; Configuration