douhong1703 2010-05-28 15:40
浏览 48
已采纳

Symfony - 同一页面上的几个表单 - > ID问题

I have an issue while displaying several forms of the same model on the same page. The problem is that with the NameFormat, the fields have the same ID :

$this->widgetSchema->setNameFormat('display[%s]');

Will display

<form class="update_display_form" id="update_display_0" action="/iperf/web/frontend_dev.php/update_display" method="post"> 
  <input type="checkbox" name="display[displayed]" checked="checked" id="display_displayed" />
  <label for="display_displayed">test</label> 
</form> 
<form class="update_display_form" id="update_display_1" action="/iperf/web/frontend_dev.php/update_display" method="post">
  <input type="checkbox" name="display[displayed]" checked="checked" id="display_displayed" />
  <label for="display_displayed">truc</label> 
</form>

And if you click on the second label, it will activate the first checkbox So I thought I could use the object id to make them unique :

$this->widgetSchema->setNameFormat('display'.$this->getObject()->getId().'[%s]');

But then I can not process the request, since I don't know the name of the parameters.

The best option I found was to set an ID :

$this->widgetSchema['displayed']->setAttributes(array("id" => "display".$this->getObject()->getId() ));

but then I totally loose the connections between the label and the checkbox.

The problem would be solved if I could change the "for" attribute of my label. Does somebody know how to do that ? Or any other option ?

  • 写回答

2条回答 默认 最新

  • duangong0690 2010-05-29 12:22
    关注

    Here's an idea... push a variable to the form class from your action for setting a different name format dynamically:

    In your action:

    $this->form_A = new displayForm(array(),array('form_id' = 'A')); // pass a form id
    $this->form_B = new displayForm(array(),array('form_id' = 'B'));
    $this->form_C = new displayForm(array(),array('form_id' = 'C'));
    

    In your form class:

    $form_id = $this->getOption('form_id'); // get the passed value
    $this->widgetSchema->setNameFormat('display'.$form_id.'[%s]'); // stick it into the name
    

    It's ugly but I'm sure you can come up with something cleaner...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 openwrt双栈NAT
  • ¥15 部分网页页面无法显示!
  • ¥15 怎样解决power bi 中设置管理聚合,详细信息表和详细信息列显示灰色,而不能选择相应的内容呢?
  • ¥15 QTOF MSE数据分析
  • ¥15 平板录音机录音问题解决
  • ¥15 请问维特智能的安卓APP在手机上存储传感器数据后,如何找到它的存储路径?
  • ¥15 (SQL语句|查询结果翻了4倍)
  • ¥15 Odoo17操作下面代码的模块时出现没有'读取'来访问
  • ¥50 .net core 并发调用接口问题
  • ¥15 网上各种方法试过了,pip还是无法使用