ferpega
ferpega

Reputation: 3224

Symfony2: entity field (html SELECT) set SELECTED item

I am using a form (filterForm) to filter entities on a twig view.

The 'filterForm' only has a field of 'entity' type. On the view it shows a HTML-SELECT-OPTIONs tag.

When the user changes the selection, the same controller is called doing the neccesary stuff to filter the entities-list.

All is working fine but I need to show the SELECT-field with the value that is filtering the list. And here is the problem, I don't know how to do it.

A bit of code of the field from the index.html.twig:

  {{ form_widget(personalFilterForm.personaFiltrarMail,
                         { 'empty_value': 'Todos',
                           'attr': {'selected': personaFiltrarMail,
                                    'onChange': 'javascript:document.filtrado.submit()' }
                         }
                )
  }}

That code is generating this html code:

    <select name="test_onebundle_type[personaFiltrarMail]" id="test_onebundle_type_personaFiltrarMail" 
onchange="javascript:document.filtrado.submit()" 
required="required" selected="[email protected]">
      <option value="">Todos</option>
      <option [email protected]">Name One</option>
      <option [email protected]">Name Two</option>
      <option [email protected]">Name three</option>

The real problem here (I think) is knowing how can I get access to the OPTIONS sub-element to set de SELECTED attribute on a concrete OPTION item.

Thanks.

=== Controller ===

Here the 'Controller'...

All four numbered 'echoes' gives me the mail: '[email protected]' But the SELECT html TAG is always located on the first one OPTION tag.

class HorasController extends Controller
{
    /**
     * Lists all Horas entities.
     *
     * @Route("/", name="horas")
     * @Template()
     */
    public function indexAction()
    {
        $em = $this->getDoctrine()->getEntityManager();

        $personas = $em->getRepository('PtGhorgaBundle:Personal')->findAll();
        $personalFilterForm = $this->createForm(new PersonalFilterType(), $personas);

        $request = $this->getRequest();
        $formVars = $request->request->get('pt_ghorgabundle_type');
        $personaFiltrarMail = $formVars['personaFiltrarMail'];
        //echo "1.- [".$personaFiltrarMail."]<br />";

        if (!isset($personaFiltrarMail) || $personaFiltrarMail=="") {
            $entities = $em->getRepository('PtGhorgaBundle:Horas')->findAll();            
        } else {
            $criterio = array('persona' => $personaFiltrarMail,);            
            $entities = $em->getRepository('PtGhorgaBundle:Horas')->findBy($criterio);
            $criterio = array('mail' => $personaFiltrarMail,);            
            $personaFiltrarMail = $em->getRepository('PtGhorgaBundle:Personal')->find($criterio)->getMail();
            echo "2.- [".$personaFiltrarMail."]<br />";
            $personalFilterForm->personaFiltrarMail = $personaFiltrarMail;   
            echo "3.- [".$personaFiltrarMail."]<br />";
            echo "4.- [".$personalFilterForm->personaFiltrarMail."]<br />";
        }

        return array('entities' => $entities, 
                     'personas' => $personas,
                     'personalFilterForm' => $personalFilterForm->createView(),
                     'personaFiltrarMail' => $personaFiltrarMail,
                    );
    }

Upvotes: 3

Views: 5771

Answers (2)

ferpega
ferpega

Reputation: 3224

I have found it:

just belown echo "4....." line

    $data = array('personaFiltrarMail'=> $personaFiltrarMail);
    $personalFilterForm->setData($data);  

Regards.

Upvotes: 2

julesbou
julesbou

Reputation: 5780

In your data you can set the property personaFiltrarMail to the according value.

For example in your controller :

$object = new Object();
$object->personaFiltrarMail = '[email protected]';
$form = $this->createFormBuilder($object);

Then render your template.

Upvotes: 2

Related Questions