dofado
dofado

Reputation: 25

Symfony 4.4 object not found by the @ParamConverter annotation

I know there are lots of similar topics already.

What I tried:

And more solutions that didn't work for me.

Here is not working fragment of the controller:

   /**
     * Edit action.
     *
     * @param Request                      $request         HTTP request
     * @param User                         $user            User entity
     * @param UserPasswordEncoderInterface $passwordEncoder Password encoder
     *
     * @return Response HTTP response
     *
     * @throws ORMException
     * @throws OptimisticLockException
     *
     * @Route(
     *     "/{id}/password",
     *     methods={"GET", "PUT"},
     *     requirements={"id": "[1-9]\d*"},
     *     name="app_password",
     * )
     */
    public function editPasswordUser(Request $request, User $user, UserPasswordEncoderInterface $passwordEncoder): Response
    {
        if (($this->getUser() == $user) || (is_array($this->getUser()->getRoles()) && in_array(
            'ROLE_ADMIN',
            $this->getUser()->getRoles()
        ))) {
            $role = $user->getRoles();
            $form = $this->createForm(NewPasswordType::class, $user, ['method' => 'PUT']);
            $form->handleRequest($request);

            if ($form->isSubmitted() && $form->isValid()) {
                $user->setPassword(
                    $passwordEncoder->encodePassword(
                        $user,
                        $user->getPassword()
                    )
                );
                $user->setUpdatedAt(new DateTime());
                $this->userService->save($user);

                $this->addFlash('success', 'message_updated_successfully');

                return $this->redirectToRoute('detail_show');
            }

            return $this->render(
                'security/password.html.twig',
                [
                    'form' => $form->createView(),
                    'user' => $user,
                    'role' => $role,
                ]
            );
        } else {
            return $this->redirectToRoute('detail_show');
        }
    }

Twig file:

{% extends 'base.html.twig' %}

{% block title %}
    {{ 'title.user_editpasswd'|trans({'%id%': user.id|default('')}) }}
{% endblock %}

{% block body %}
    <h1>{{ 'title.user_editpasswd'|trans({'%id%': user.id|default('')}) }}</h1>
    {{ form_start(form, { method: 'PUT', action: url('app_password', {id: user.id}) }) }}
    {{ form_widget(form) }}
    <div class="form-group row float-sm-right">
        <input type="submit" value="{{ 'action.save'|trans }}" class="btn btn-primary" />
    </div>
    <div>
        {% if role[0] == 'ROLE_ADMIN' %}
            <a href="{{ url('user_view', {id: user.id} ) }}" title="{{ 'action.back_to_view'|trans }}">
                {{ 'action.back_to_view'|trans  }}
            </a>
        {% endif %}
    </div>
    <div>
        {% if role[0] == 'ROLE_ADMIN' %}
            <a href="{{ url('user_index') }}" title="{{ 'action.index'|trans }}">
                {{ 'action.index'|trans  }}
            </a>
        {% endif %}
    </div>
    {{ form_end(form) }}
{% endblock %}

My app is almost finished, and that's the first and only time I have such an error.

Upvotes: 0

Views: 2360

Answers (1)

Dmitry Solovov
Dmitry Solovov

Reputation: 333

Change argument type in controller action from User to \Symfony\Component\Security\Core\User\UserInterface and everything should work. Current user is registered in a container by this service id.

Upvotes: 1

Related Questions