1

When you embed a form in Symfony, a label for the embedded form is injected into the parent form. For example: If I have a PersonForm and add this code $this->embedForm('Address', $addressForm), my PersonForm will now have an 'Address' label within it, in addition to the labels for the fields that make up the AddressForm. I'd like to keep the labels for the individual fields, but get rid of the 'Address' label, thereby making it appear that the two forms are really one.

It's possible to override the form template and manually iterate over the form elements and echo them one by one, but I run into this situation frequently and I'd prefer to have Symfony handle this automatically.

3 Answers 3

3

The following code will allow you to set the label to another an empty string but I suspect it would still appear anyway.

$this->embedForm('Address', $addressForm)
$this->widgetSchema['Address']->setLabel('');

however I suspect the best thing to use is to look at point 6 (embedMergeForm) on this page and use that http://itsmajax.com/2011/01/29/6-things-to-know-about-embedded-forms-in-symfony/

Sign up to request clarification or add additional context in comments.

5 Comments

I tried setting the label to blank (it actually needs to be ' '), but the label element was still in the HTML, causing a line break. I'm taking a look at your link now...
Setting the label to false will prevent it from rendering completely. However, that doesn't help here as Arms needs to hide the labels on each widget of the embedded form.
@jeremy I just tried setting the 'parent' label to false and it worked perfectly. Mind editing your answer so I can accept it?
That link was an interesting read, but unfortunately my embedded objects didn't save properly. More worrisome were the comments other people left about many-to-many relationships not working.
Do you have many to many relationship for this. That does lead to a whole new set of problems to be honest.
2

Given your situation, iterating manually over the widgets is the only option. The other option is extending sfWidgetFormSchemaFormatter, but that won't allow hiding a label for an embedded form, while at the same time not hiding it for any other widget.

If you do run into this situation often, you could consider creating a partial just for rendering your form in this specific way.

Comments

1

Here is a simple way of disabling all labels. Add this method to BaseForm if you do it frequently.

public function disableLabels()
{
  $fields = $this->getWidgetSchema()->getFields();
  $this->getWidgetSchema()->setLabels(array_combine(array_keys($fields), array_fill(0, count($fields), false)));
}

If you only want to disable labels in the embedded form, disable them before embedding:

$form = new FormToEmbed();
$form->disableLabels();
$parent->embedForm('child', $form);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.