Symfony2:实体字段(html SELECT)设置所选项目

发布于 2024-11-29 07:27:38 字数 3702 浏览 2 评论 0原文

我正在使用表单(filterForm)来过滤树枝视图上的实体。

“filterForm”只有一个“entity”类型的字段。在视图上它显示一个 HTML-SELECT-OPTIONs 标签。

当用户更改选择时,将调用相同的控制器来执行必要的操作来过滤实体列表。

一切正常,但我需要显示带有过滤列表的值的 SELECT 字段。这就是问题所在,我不知道该怎么做。

来自index.html.twig的字段代码:

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

该代码正在生成此html代码:

    <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>

这里真正的问题(我认为)是知道如何访问 OPTIONS 子元素以设置 de SELECTED 属性具体的选项。

谢谢。

=== 控制器 ===

这里的“控制器”...

所有四个编号的“回声”给我邮件:“[email protected]' 但 SELECT html 标记始终位于第一个选项标签。

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,
                    );
    }

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,
                    );
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枫林﹌晚霞¤ 2024-12-06 07:27:38

在您的数据中,您可以将属性 personaFiltrarMail 设置为相应的值。

例如在您的控制器中:

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

然后渲染您的模板。

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.

赤濁 2024-12-06 07:27:38

我找到了:

就在下面
回显“4.....”行

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

问候。

I have found it:

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

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

Regards.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文