1

I am in troubles while trying to pass parameters from my form to my controller.

What I am trying to implement is a list of choices that will permit to display a list of users depending on parameters (role, ban ...).

The problem is I think that I don't get the parameter after submit to have my request in index. Could the problem be that I reder the same form in the same function before and after submitting ?

I tried putting href/onclick/ tags on my options but it didn't work.

index.html.twig

<form class="form-group" action="{{ path('user_index') }}">
            <table class="table">
                <tr>
                    <td>
                        <select class="custom-select">
                            <option selected="">Which users to you want to see ?</option>
                            <option value="1">All</option>
                            <option value="2">Students</option>
                            <option value="3">Teachers</option>
                            <option value="4">Admins</option>
                            <option value="5">Super Admin</option>
                            <option value="6">Banned</option>
                            <option value="7">Unbanned</option>
                        </select>
                    </td>
                    <td>
                        <input type="submit" value="Submit">
                    </td>
                </tr>
            </table>
        </form>
UserController.php

/**
     * Requires ADMIN_ROLE
     *
     * @Route("/", name="user_index", methods={"GET"})
     */
    public function index(Request $request): Response
    {
        $this->denyAccessUnlessGranted('ROLE_ADMIN');

        $ban = true;

        // trying to get the value of the option in form ?
        $value = $request->request->get('value');

        switch ($value){
            case 1 :
                $users = $this->getDoctrine()
                    ->getRepository(User::class)
                    ->findAll();
                break;
            case 6 :
                $users = $this->getDoctrine()
                    ->getRepository(User::class)
                    ->findByBan($ban);
                break;
            case 7 :
                $ban = false;
                $users = $this->getDoctrine()
                    ->getRepository(User::class)
                    ->findByBan($ban);
                break;
            default:
                $users = $this->getDoctrine()
                    ->getRepository(User::class)
                    ->findAll();
                break;
        }

        return $this->render('user/index.html.twig', [
            'title' => 'User Index',
            'headtitle' => 'View all the users list',
            'users' => $users,
        ]);
    }
3
  • initialize $users global outer form switch statement.... Commented May 15, 2019 at 12:50
  • oh yeah you're rigth .. Commented May 15, 2019 at 12:55
  • the <select> should really have a name attribute. you surely can call it non-descriptivly "value", but I would advise for something more sensible like ... <select name="usergroup"> Commented May 15, 2019 at 12:55

2 Answers 2

4

As I understand, the method of your form is a GET request. To retrieves $_GET variable using the HttpFoundation component you need to:

  1. Your select input should have a name.

Example:

<form class="form-group" action="{{ path('user_index') }}" method="get">
            <table class="table">
                <tr>
                    <td>
                        <select class="custom-select" name="users">
                            <option selected="">Which users to you want to see ?</option>
                            <option value="1">All</option>
                            <option value="2">Students</option>
                            <option value="3">Teachers</option>
                            <option value="4">Admins</option>
                            <option value="5">Super Admin</option>
                            <option value="6">Banned</option>
                            <option value="7">Unbanned</option>
                        </select>
                    </td>
                    <td>
                        <input type="submit" value="Submit">
                    </td>
                </tr>
            </table>
</form>
  1. Try to fetch your request (GET) parameters by using $request->query->get('users')

Check: Symfony Request Object

Sign up to request clarification or add additional context in comments.

Comments

4

your select doesn't have a name:

<select class="custom-select" name="userselect">
    <option selected="">Which users to you want to see ?</option>
    <option value="1">All</option>
    <option value="2">Students</option>
    <option value="3">Teachers</option>
    <option value="4">Admins</option>
    <option value="5">Super Admin</option>
    <option value="6">Banned</option>
    <option value="7">Unbanned</option>
</select>

and in your controller get the "userselect" in $request.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.