Skip to content

Commit eecc812

Browse files
vicbweaverryan
authored andcommitted
Add some notes on how to handle multi-dimensional fields
1 parent 6104f40 commit eecc812

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

components/dom_crawler.rst

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ instance with just the selected link(s). Calling ``link()`` gives us a special
183183

184184
$linksCrawler = $crawler->selectLink('Go elsewhere...');
185185
$link = $linksCrawler->link();
186-
186+
187187
// or do this all at once
188188
$link = $crawler->selectLink('Go elsewhere...')->link();
189189

@@ -213,7 +213,7 @@ a :class:`Symfony\\Component\\DomCrawler\\Form` object that represents the
213213
form that the button lives in::
214214

215215
$form = $crawler->selectButton('validate')->form();
216-
216+
217217
// or "fill" the form fields with data
218218
$form = $crawler->selectButton('validate')->form(array(
219219
'name' => 'Ryan',
@@ -223,7 +223,7 @@ The :class:`Symfony\\Component\\DomCrawler\\Form` object has lots of very
223223
useful methods for working with forms:
224224

225225
$uri = $form->getUri();
226-
226+
227227
$method = $form->getMethod();
228228

229229
The :method:`Symfony\\Component\\DomCrawler\\Form::getUri` method does more
@@ -245,12 +245,31 @@ You can virtually set and get values on the form::
245245
// returns the values like PHP would see them, where "registration" is its own array
246246
$values = $form->getPhpValues();
247247

248+
To work with multi-dimensional fields::
249+
250+
<form>
251+
<input name="multi[]" />
252+
<input name="multi[]" />
253+
<input name="multi[dimensional]" />
254+
</form>
255+
256+
You must specify the fully qualified name of the field::
257+
258+
// Set a single field
259+
$form->setValue('multi[0]', 'value');
260+
261+
// Set multiple fields at once
262+
$form->setValue('multi', array(
263+
1 => 'value',
264+
'dimensional' => 'an other value'
265+
));
266+
248267
This is great, but it gets better! The ``Form`` object allows you to interact
249268
with your form like a browser, selecting radio values, ticking checkboxes,
250269
and uploading files::
251270

252271
$form['registration[username]']->setValue('symfonyfan');
253-
272+
254273
// check or uncheck a checkbox
255274
$form['registration[terms]']->tick();
256275
$form['registration[terms]']->untick();
@@ -278,7 +297,7 @@ of the information you need to create a POST request for the form::
278297
$method = $form->getMethod();
279298
$values = $form->getValues();
280299
$files = $form->getFiles();
281-
300+
282301
// now use some HTTP client and post using this information
283302

284303
One great example of an integrated system that uses all of this is `Goutte`_.

0 commit comments

Comments
 (0)