/** * Merges a field group into this group. The group must have a unique name * within the group. Otherwise the existing field is overwritten. * * Contrary to added groups, merged groups operate on the same object as * the group they are merged into. * * <code> * class Entity * { * public $longitude; * public $latitude; * } * * $entity = new Entity(); * * $form = new Form('entity', $entity, $validator); * * $locationGroup = new FieldGroup('location'); * $locationGroup->add(new TextField('longitude')); * $locationGroup->add(new TextField('latitude')); * * $form->merge($locationGroup); * </code> * * @param FieldGroup $group */ public function merge(FieldGroup $group) { if ($group->isBound()) { throw new AlreadyBoundException('A bound form group cannot be merged'); } foreach ($group as $field) { $group->remove($field->getKey()); $this->add($field); if (($path = $group->getPropertyPath()) !== null) { $field->setPropertyPath($path . '.' . $field->getPropertyPath()); } } return $this; }
public function testRemoveUnsetsFieldParent() { $group = new FieldGroup('author'); $field = $this->createMockField('firstName'); $field->expects($this->exactly(2))->method('setParent'); // PHPUnit fails to compare subsequent method calls with different arguments $group->add($field); $group->remove('firstName'); }