简体   繁体   English

Symfony-同一页面上的多种表单-> ID问题

[英]Symfony - Several form on the same page -> ID issue

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 : 问题是使用NameFormat时,字段具有相同的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 : 如果单击第二个标签,它将激活第一个复选框,所以我想我可以使用对象ID使其具有唯一性:

$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 : 我发现最好的选择是设置一个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. 如果我可以更改标签的“ for”属性,则可以解决该问题。 Does somebody know how to do that ? 有人知道该怎么做吗? Or any other option ? 还是其他选择?

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... 很难看,但我敢肯定您可以拿出更清洁的东西...

Conflicting inter-form checkbox/label interactions is caused by tag's id/for attributes not by their name attributes. 表单之间的复选框/标签交互作用冲突是由标签的ID /属性引起的,而不是由其名称属性引起的。

So there is no need to modify form's widget name format and thus having problem reading submitted data from request object (either by passing request key as form url parameter/hidden input or by looping all form name combinations created in the layout for each form and finding a matching one). 因此,无需修改表单的窗口小部件名称格式,从而不必从请求对象中读取提交的数据(通过将请求密钥作为表单url参数/隐藏输入传递,或者通过循环在布局中为每个表单创建的所有表单名称组合并查找匹配的)。

sfForm class has sfWidgetFormSchema::setIdFormat() method for that. sfForm类具有sfWidgetFormSchema :: setIdFormat()方法。

// Creating form instances

$formA = new sfForm();
$formA->getWidgetSchema()->setIdFormat( '%s1' );
$formA->getWidgetSchema()->setNameFormat( 'display' );
... // configure the form

$formB = new sfForm();
$formB->getWidgetSchema()->setIdFormat( '%s2' );
$formB->getWidgetSchema()->setNameFormat( 'display' );
... // configure the form

$formC = new sfForm();
$formC->getWidgetSchema()->setIdFormat( '%s3' );
$formC->getWidgetSchema()->setNameFormat( 'display' );
... // configure the form


// Processing a request data

$form = new sfForm();
... // configure the form
$_formNameRequestKey = $form->getName();
if( $request->hasParameter( $_formNameRequestKey ) ) {
  $form->bind( $request->getParameter( $_formNameRequestKey ) );
}

... or just ...
if( $request->hasParameter( 'display' ) ) {
  $form->bind( $request->getParameter( 'display' ) );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM