2

I just got into Symfony2 as an ASP.NET programmer. I have this very simple form that I want to put in. Since it only contains a label and an input, I figured I didn't want to make a separate form class for it. But as soon as I tried to preform that, I ran into some trouble:

<form action="{{ path('search', [{'src':'bla'}]) }}" method="get">
    <label for="src"></label>
    <input type="text" name="src" id="src" class="form-control search-input" placeholder="Type a group or song name: ie. Metallica, Fly me to the moon etc.">
</form>

How do I set the path to mydomain.com/search/{src}, I'm trying to at least build a path for now, so I'm using 'bla' as some placeholder valye. Right now from the code above, I receive the following error upon loading the page that contains the form:

An exception has been thrown during the rendering of a template ("Some mandatory parameters are missing ("src") to generate a URL for route "search".") in :home:index.html.twig

Some methods I also tried:

path('search', 'bla')   <-- and --> path('search', 'bla', [])

brings error:

An exception has been thrown during the rendering of a template ("Warning: array_replace(): Argument #3 is not an array") in :home:index.html.twig

path('search', ['src':'bla'])

and some more (probably) nonsense that didn't get me close to the result.

Is this even doable, or do I have to do it the 'normal' way with {{ form_start() }} and form classes?

Toza
  • 1,348
  • 2
  • 14
  • 35

1 Answers1

3

Don't try, just read documentation, it's more effective and faster :) http://symfony.com/doc/current/book/templating.html#book-templating-pages

{{ path('search', {'src':'bla'}) }}
malcolm
  • 5,486
  • 26
  • 45
  • Yeah, it brings the page back. Any idea how I could substract the value from the input instead of the 'bla' constantly showing up? Currently my url loads up as: `http://localhost:8000/search/bla?src=bla`? I'll give you the check, regardless, just a side question. – Toza Sep 13 '15 at 16:52
  • 1
    You must have a route with exact `src` parameter: `/search/{src}`, any other parameters not included in route will display like `?param=bla&another_param=bla` – malcolm Sep 13 '15 at 16:53
  • Or if I type something unrelated, it appears as `http://localhost:8000/search/bla?src=bazoom` edit: I did. Give me a sec edit: this is it: `search: path: /search/{src} defaults: { _controller: AppBundle:Home:search }` – Toza Sep 13 '15 at 16:54
  • 1
    ok, I know what you mean, it's default behaviour of forms. You can make route like `/search/?src={src}` and in your form action just path to search route without parameter. Another way is to redirect from `/search?scr=keyword` to `search/keyword`, but you must implement another action. – malcolm Sep 13 '15 at 17:07
  • Ah, yeah, I see what you mean. I think I'll stay with ?sec= for now then. I'll work around routing later on. Thank you very much for the additional answers! :) – Toza Sep 13 '15 at 17:12
  • 1
    Thanks, look at those answers, maybe help you a little: http://stackoverflow.com/a/22430651/3675759 http://stackoverflow.com/a/23114632/3675759 – malcolm Sep 13 '15 at 17:16