php - Access to variable in controller and render -
i have on form template new.html.twig
select elements of entity loaded ($categories
).
controller
public function newaction(request $request){ $entity = new playlist(); $form = $this->createcreateform($entity); $em = $this->getdoctrine()->getmanager(); $categories = $em->getrepository('publicartelappbundle:category')->getallcategories();** return $this->render('publicartelappbundle:playlist:new.html.twig', array( 'categories' => $categories, 'entity' => $entity, 'form' => $form->createview(), )); }
select filter serves make search.
select in twig template displays categories obtained query.
new.html.twig
<div class="form-group"> <label for="publicartel_appbundle_category_name">search category: </label> <select id='selectcategory' class="form-control select2"> {% category in categories %} <option> {{ category.name }} </option> {% endfor %} </select> </div>
js in twig template detects changes in select option make new query , retrieve records selected option.
js code
$('#selectcategory').on('change', function () { var optionselect = $(this).val(); $.post("{{path('playlist_category') }}", {category: optionselect}, function (filtercontent) { (var content in filtercontent.category_result) { (var name in filtercontent.category_result[content]) { var result = filtercontent.category_result[content][name]; console.log(result); $('#playlist_content_name').append('<option>' + result + '</option>'); } } }, 'json'); });
the result of new query done in playlist_category
route assigned select in twig template append method.
<div class="form-group"> <label for="publicartel_appbundle_area_name">content</label> {{ form_widget(form.items.vars.prototype.content, { 'id': 'playlist_content_name', 'full_name': '', required: false, 'attr': {'class': 'form-control select2'}}) }} </div>
this select load other options, other select used filter options prevent options loaded.
my problem when try send form of options obtained search, error variable categories
not exist in template twig new.html.twig
.
then, method createaction, conducts rendering of template new.html.twig
add variable $ categories there.
controller
public function createaction(request $request){ $entity = new playlist(); $form = $this->createcreateform($entity); $form->handlerequest($request); $em = $this->getdoctrine()->getmanager(); if ($form->isvalid()) { // $em = $this->getdoctrine()->getmanager(); $em->persist($entity); $em->flush(); return $this->redirect($this->generateurl('playlist_show', array( 'id' => $entity->getid()))); } $categories = $em->getrepository('publicartelappbundle:category')->getallcategories(); return $this->render('publicartelappbundle:playlist:new.html.twig', array( 'categories' => $categories, 'entity' => $entity, 'form' => $form->createview(), )); }
but when complete form, having chosen option of select return search, instead of addressing playlist_show
route, directs me path /create
form new.html.twig
.
Comments
Post a Comment