Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit41276ff

Browse files
Tests: Explain how to add or remove data in a collection of forms
1 parent6a55732 commit41276ff

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)

[8]ページ先頭

©2009-2025 Movatter.jp