views:

38

answers:

1

Hi folks.

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 ?

+1  A: 

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

Tom
Hmmm that's pretty interesting, thank you. But when I process the form, how do I get the form ID that has been submitted? Currently I use the $request->getParameter('display'); but if the name is dynamically assigned... I could do: letter_array = array('A','B',...);while(!$request->getParameter('display'.letter_array[$i])) {$i++} but this starts to be seriously ugly no ?
Julien
To get the form id that has been submitted, you could either link the 'form_id' variable to the submitted POST request purely in the action, or you could add a hidden input field that submits anything you need it to via the form class.... I think there's some options.
Tom
I hadn't seen your comment, that is the cleanest way I can think about. Thanks
Julien