mickburkejnr
mickburkejnr

Reputation: 3690

Pass parameters from form submission to specified route in Syfmony2

I have a route in my routing.yml which is as follows:

pattern:  /admin/clients/{clientid}/projects/{projectid}/review/{id}/comments
defaults: { _controller: ShoutAdminBundle:Project:revcomm }
requirements:
    _method:  POST

Now, on a page (the comments page) I have a form where the user will enter a comment and then submit it to that page. Then the user clicks submit, they will be brought back to the same page but their comment will have been submitted to the database and then displayed.

My Form code looks like this:

        <form action="{{ path('ShoutAdminBundle_adminprojectreviewcommentssuccess') }}" method="post" {{ form_enctype(form) }} class="blogger">
            {{ form_errors(form) }}

            <p class="comments">
                {{ form_label(form.from, 'Your Name*', { 'attr': {'class': 'title'} }) }}
                {{ form_errors(form.from) }}
                {{ form_widget(form.from, { 'attr': {'class': 'textfield'}}) }}
            </p>
            <p class="comments">
                {{ form_label(form.comment, 'Your Comment*', { 'attr': {'class': 'title'} }) }}
                {{ form_errors(form.comment) }}
                {{ form_widget(form.comment, { 'attr': {'class': 'textfield'}}) }}
            </p>
            <p class="comments_save">
                <input type="submit" value="Post Your Comment" id="savebutton" class="savebutton" />
            </p>
            {{ form_rest(form) }} 
        </form>

Now. When the page is rendered I get the following error:

An exception has been thrown during the rendering of a template ("The "ShoutAdminBundle_route" route has some missing mandatory parameters ("id", "projectid").") in "ShoutAdminBundle:Default:projectreviewcomments.html.twig" at line 54.

How do I pass the variables {clientid}, {projectid} and {id} to the page? These variable are declared in the page already, it's a question of how do I include them in the form submission?

Cheers

Upvotes: 1

Views: 6066

Answers (1)

kgilden
kgilden

Reputation: 10356

You can pass them via your controller by using an associative array:

class ProjectController extends Controller
{
    public function revcommAction($clientid, $projectid, $id)
    {
        // ...

        $params = array(
            'clientid'  => $clientid,
            'projectid' => $projectid,
            'id'        => $id
        );
        return $this->render('ShoutAdminBundle:Default:projectreviewcomments.html.twig', $params);
    }
}

To render a link (or a path for the form action) to this controller in a template, you can use path() for relative and url() for absolute links. The second argument takes in any arguments required to construct the link, for example:

<a href="{{ path('my_route_name', {'clientid': clientid, ...}) }}"></a>

Upvotes: 4

Related Questions