Skip to content
Advertisement

Cannot autowire argument $of “AppControllerHomeController”: it references class “SymfonyComponentFormSubmitButton” but no such service exists

symfony noob here.

Im not sure what this autowiring error is about.

I am trying to check if a button is clicked (maybe there is a better way to do this)

Like so(see first if)

 if ($button->get('submit')) {
        $parameters = [];

        $clientId = $request->get('client_id');
        $clientSecret = $request->get('client_secret');
        $playlistId = $request->get('playlist_id');

        if ($clientId && $clientSecret && $playlistId) {
            $parameters['client_id'] = $playlistId;
            $parameters['client_secret'] = $clientSecret;
            $parameters['playlist_id'] = $playlistId;
        }
        Validator::validateArrayKeys($parameters);
    }

This is my whole class, am i referencing something wrong here ? Or is there another way to check if a form has been submitted (I dont have a database)

  /**
 * @Route("/", name="app_index")
 */
public function index(Request $request, SubmitButton $button)
{

    $this->getParameters($request, $button);

    return $this->render('home/index.html.twig', [
        'controller_name' => 'HomeController',
    ]);
}


public function getParameters(Request $request, SubmitButton $button)
{
    $submitButton = $button->get('submit');

    dd($submitButton);

    if ($button->get('submit')) {
        $parameters = [];

        $clientId = $request->get('client_id');
        $clientSecret = $request->get('client_secret');
        $playlistId = $request->get('playlist_id');

        if ($clientId && $clientSecret && $playlistId) {
            $parameters['client_id'] = $playlistId;
            $parameters['client_secret'] = $clientSecret;
            $parameters['playlist_id'] = $playlistId;
        }
        Validator::validateArrayKeys($parameters);
    }
}

Advertisement

Answer

The problem is here:

public function index(Request $request, SubmitButton $button)

Symfony can not infer what $button is. If you have a corresponding form then you should instantiate that form, handle the request and then you can check if the button was clicked, roughly like this:

public function index(Request $request)
{
    $form = $this->createForm(MyFormType::class);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        if ($form->get('submit')->isClicked()) {
            // ...
        } else {
            // ...
        }
    }

    return $this->render('my_template.html.twig', ['form' => $form->createView()]);
}

Understandably that is a bit much if all you want to do is check if a button was clicked and you don’t want to build a whole form around this. For this you can just access the submitted data from the request without having to resort to any form class whatsover:

public function index(Request $request)
{
    $isClicked = $request->request->get('submit');

    // ...
}

Instead of $request->request this can be $request->query if you submitted the value using GET-method, rather than post.

See also:

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement