1

Given a Type that generate this kind of HTML:

<div class="form-check">
    <input
            type="checkbox" 
            id="app_user_responsibilities_14" 
            name="app_user[responsibilities][]" 
            data-responsibility-description="A description." 
            class="form-check-input" 
            value="14">
        <label class="form-check-label" for="app_user_responsibilities_14">Checkbox label</label>
</div>

How can I specifically tick it using the Symfony DOM crawler?

I tried:

    $form->setValues([
        'app_user[username]' => $username,
        'app_user[responsibilities]' => [
            14
        ],
    ]);

14 being the Responsibility id and the value of the input.

But I got an error when I run the test:

1) Tests\App\Controller\UserControllerTest::testCreateUserWithResponsibility14
InvalidArgumentException: Input "app_user[responsibilities][]" cannot take "14" as a value (possible values: 8).
LucileDT
  • 546
  • 1
  • 6
  • 22
  • 1
    i don't claim to be a symphony expert, but searching using google with keywords `set checkbox symphony dom crawler` yielded this results https://stackoverflow.com/a/16175416/3859027 there is a `->tick()` method and you can even select which specific checkbox. – Kevin Aug 07 '19 at 03:16
  • even in the documentation, they have https://symfony.com/doc/current/components/dom_crawler.html#forms – Kevin Aug 07 '19 at 03:18
  • I read the documentation, but thank you. `$form['niwa_pictbundle_proxytype[chronologyControls]'][0]->tick();` use the index of the input in an array, not the value of the input. In my case, the index is not the same as the value. – LucileDT Aug 07 '19 at 03:31
  • That's why I can't use the tick method. Trust me if I could I would already done it. – LucileDT Aug 07 '19 at 03:32
  • the `->tick` method is already available for use, just use it in conjunction with `->filter` to pinpoint using the actual value, not the index – Kevin Aug 07 '19 at 03:34

1 Answers1

1

As I have an index different from the value, I need to use the disableValidation function in options :

$form->disableValidation()
    ->setValues([
        'app_user[username]' => $username,
        'app_user[responsibilities]' => [
            14,
    ],
]);
LucileDT
  • 546
  • 1
  • 6
  • 22