0

I have a checkbox form element like this:

            ...
            ->add(
                'race',
                'choice',
                array(
                    'label' => 'Racial Identity',
                    'choices' => array(
                        'American Indian or Alaskan Native' => 'American Indian or Alaskan Native',
                        'Asian' => 'Asian',
                        'Black or African American' => 'Black or African American',
                        'Native Hawaiian or Other Pacific Islander' => 'Native Hawaiian or Other Pacific Islander',
                        'White' => 'White',
                    ),
                    'multiple' => true,
                    'expanded' => true,
                )
            )
            ...

I'm writing a test to submit this form with a couple of the options checked:

    ...
    $form = $crawler->selectButton('Submit Application')->form(array(
        'application' => array(
            ...
            'race' => array(
                'Asian',
                'White',
            ),
            ...
        )
    ));
    $crawler = $client->submit($form);

When submitting the form, I get this error:

InvalidArgumentException: Input "application[race][]" cannot take "Asian" as a value (possible values: American Indian or Alaskan Native).

According to the docs on the DOM Crawler, I'm properly passing the data to the multi-dimensional form.

Here's a what the HTML looks like for the checkboxes:

<input type="checkbox" id="application_race_0" name="application[race][]" value="American Indian or Alaskan Native">
<input type="checkbox" id="application_race_1" name="application[race][]" value="Asian">
<input type="checkbox" id="application_race_2" name="application[race][]" value="Black or African American">
<input type="checkbox" id="application_race_3" name="application[race][]" value="Native Hawaiian or Other Pacific Islander">
<input type="checkbox" id="application_race_4" name="application[race][]" value="White">

Why isn't this working? It appears to think that only the first checkbox is valid.

Brian
  • 7,204
  • 12
  • 51
  • 84

1 Answers1

0

Turns out I need to use proper numeric indexes in the array of values which correspond with the order the checkboxes appear.

This works:

            ...
            'race' => array(
                1 => true,
                2 => false,
            ),
            ...

Or I can do this:

            $form['application[race]'][1]->tick();
            $form['application[race]'][2]->tick();

After some more googling this morning I came upon this post that helped me:

Symfony2 functional test to select checkboxes

Community
  • 1
  • 1
Brian
  • 7,204
  • 12
  • 51
  • 84