0
votes

Hey I am trying to get the selected option value in my route (annotation). I have Portfolio Controller which contains Index view. Inside the index view there is a form with only one select drop down. Initially the URL looks like

http://localhost/idp/web/app_dev.php/portfolio/ 

The form inside the index view is

<form name="portfolios" action="{{ path('v2_pm_portfolio_switch') }}" method="post" >
    <select name="portfolio" style="width: 200px; height:25px;">
        <option selected="selected" value="default">Switch Your Portfolio</option>
        {% for portfolio in portfolios %}
            <option value={{ portfolio.id }}>{{ portfolio.portfolioName }}</option>
        {% endfor %}
    </select>
   <input type="submit"class="portfolio_input button2 tooltip" value="Switch">
</form>

When the user submit the form, it calls the switchportfolio Action (inside portfolio controller) My switch portfolio Action is

/**
* Switch Portfolio action.
* @Route("/{user selected option name should come here}", name="v2_pm_portfolio_switch")
* @Secure(roles="ROLE_Normal_Registered_User")
* @Template("MunichInnovationGroupPatentBundle:Portfolio:index.html.twig")
*/
public function switchportfolioAction(Request $request){
    }

How can I send the selected option name to my URL in Symfony2 ? So that, if the user select portofolio1, and submit the form the URL becomes :

http://localhost/idp/web/app_dev.php/portfolio/portfolio1
3

3 Answers

0
votes

HTML forms do not behave like that. You'll need to add some javascript, that runs when the form is submitted, that stops the submission and changes the current location according to the selected option.

0
votes

Do something like this:

/**
 * Switch Portfolio action.
 * @Route("/v2_pm_portfolio_switch", name="v2_pm_portfolio_switch")
 * ...
 */
public function switchPortfolioAction(Request $request){
    //check that the form parameters are OK and get the portfolio id from the request
    return $this->redirect($this->generateUrl('show_portfolio',array('portfolio'=>$portfolioId)));                

}

/**
 * Show Portfolio action.
 * @Route("/portfolio/{portfolio)", name="show_portfolio")
 * ...
 */
public function showPortfolioAction($portfolio){
    //do whatever you need to show this portfolio
}
0
votes

I think this is not the right way to do so.

What I mean is that if you only want to have a form with a select that modifies the URL, you should not use POST as the form submit method. That would be more fitting for a GET method then.

This is to my opinion, not a Symfony2 question then.

You could either do like @carlos-granados is refering to, but instead of creating a forward, grab the submit event, and use javascript to redirect using window.location way described here

Adjust form

You could just drop the <form> action="" attribute and only fill each

<option value="{{ path('show_portfolio', { 'slug': portfolio.name }}">{{ portoflio.name }}</option>

Nodes which will generate the proper fully generated path. Then, make sure your show_portfolio has the {slug} ready to get the entry from the database with the proper

Adjust Controller method

$entry = $em->getRepository('Bundle:Portoflio')->findBy(array('slug'=>$request->get('slug')));

But this is where I was going with my original answer. You should look for @ParamConverter and see for something similar to here

Then add javascript

$('form').submit(function(event){ 
    event.preventDefault(); 
    window.location.href = $('form select').value(); 
});