Symfony嵌入式表单 - 可能不显示嵌入式表单的“父”标签?

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.

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

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.

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/