Question

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 ?

Was it helpful?

Solution

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

OTHER TIPS

Conflicting inter-form checkbox/label interactions is caused by tag's id/for attributes not by their name attributes.

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

sfForm class has sfWidgetFormSchema::setIdFormat() method for that.

// 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' ) );
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top