mercredi 29 juillet 2020

How to Test a Form with Multiple Submit

I have a form with 2 submit

// src/Form/FooType.php
$builder
    ->add('mainsubmit', SubmitType::class, [])
    ->add('extrasubmit', SubmitType::class, [])

In my controller, I do some different treatment depending of the submit pressed

// src/Controller/FooController.php
if ($form->isSubmitted() && $form->isValid()) {
    if ($form->get('extrasubmit')->isClicked()) {
        // do some extra stuff
    }

When I click on the extra button, I can see it the symfony Profiler in the request POST parameters "extrasubmit" => "".
Everything works fine.

I'm doing functional tests with the crawler. Without trying to submit with the extra submit, it works fine, so we can assume my test doesn't have a typo.

How can I simulate the click on the extra submit ?

First Try:

$form = $crawler->filter('form')->form();
// [...]
$form['my_form_name[extrasubmit]'] = true;
$httpClient->submit($form);
// => InvalidArgumentException: Unreachable field "extrasubmit"

Second Try:

$form->get('my_form_name[extrasubmit]')->setValue("");
// => InvalidArgumentException: Unreachable field "extrasubmit"

Aucun commentaire:

Enregistrer un commentaire