add() public method

If you add a nested group, this group should also be represented in the object hierarchy. If you want to add a group that operates on the same hierarchy level, use merge(). class Entity { public $location; } class Location { public $longitude; public $latitude; } $entity = new Entity(); $entity->location = new Location(); $form = new Form('entity', $entity, $validator); $locationGroup = new FieldGroup('location'); $locationGroup->add(new TextField('longitude')); $locationGroup->add(new TextField('latitude')); $form->add($locationGroup);
public add ( Symfony\Component\Form\FieldInterface $field )
$field Symfony\Component\Form\FieldInterface
Beispiel #1
0
 /**
  * {@inheritDoc}
  *
  * @throws FormException  When the field is in mode HybridField::FIELD adding
  *                        subfields is not allowed
  */
 public function add($field)
 {
     if ($this->mode === self::FIELD) {
         throw new FormException('You cannot add nested fields while in mode FIELD');
     }
     return parent::add($field);
 }
 public function testCollectionOfFieldGroupsBoundWithArrayObjectContainingObjects()
 {
     $fieldGroup = new FieldGroup('name');
     $fieldGroup->add(new TestField('first'));
     $fieldGroup->add(new TestField('last'));
     $field = new CollectionField($fieldGroup);
     $nameData = (object) array('first' => 'Foo', 'last' => 'Bar');
     $collectionData = new \ArrayObject(array($nameData));
     $field->setData($collectionData);
     $boundNameData = (object) array('first' => 'Foo', 'last' => 'Baz');
     $boundCollectionData = new \ArrayObject(array($nameData));
     $field->bind($boundCollectionData);
     $this->assertTrue($field->has('0'));
     $this->assertFalse($field->has('1'));
     $this->assertEquals($boundNameData, $field[0]->getData());
 }
Beispiel #3
0
 /**
  * Create a group containing two fields, "visibleField" and "hiddenField"
  *
  * @return FieldGroup
  */
 protected function getGroupWithBothVisibleAndHiddenField()
 {
     $group = new FieldGroup('testGroup');
     // add a visible field
     $visibleField = $this->createMockField('visibleField');
     $visibleField->expects($this->once())->method('isHidden')->will($this->returnValue(false));
     $group->add($visibleField);
     // add a hidden field
     $hiddenField = $this->createMockField('hiddenField');
     $hiddenField->expects($this->once())->method('isHidden')->will($this->returnValue(true));
     $group->add($hiddenField);
     return $group;
 }
 public function configure()
 {
     $contact = $this->getData();
     $this->addOption('company_choices');
     $this->addRequiredOption('entity_manager');
     $em = $this->getOption('entity_manager');
     $this->add(new TextField('name'));
     if ($contact->getType() == 1) {
         $this->add(new TextField('title'));
         $this->addRequiredOption('company_choices');
         $contactTransformer = new EntityToIDTransformer(array('em' => $em, 'className' => 'Application\\ChiaBundle\\Entity\\Contact'));
         $companyField = new AutocompleteField('company', array('choices' => $this->getOption('company_choices')));
         $companyField->setValueTransformer($contactTransformer);
         $this->add($companyField);
     }
     $this->add(new TextField('code'));
     $this->add(new TextareaField('description'));
     // phone
     $phonesTransformer = new CollectionToGroupTransformer(array('em' => $em, 'fields' => array('number'), 'className' => 'Application\\ChiaBundle\\Entity\\Phonenumber', 'create_instance_callback' => function () use($contact, $em) {
         $phone = new Phonenumber();
         $contact->addPhonenumbers($phone);
         $phone->setContact($contact);
         $em->persist($phone);
         return $phone;
     }, 'remove_instance_callback' => function ($phone) use($contact, $em) {
         $contact->getPhonenumbers()->removeElement($phone);
         $em->remove($phone);
     }));
     $phoneGroup = new FieldGroup('phonenumbers');
     $phoneGroup->add(new TextField('number'));
     $phones = new CollectionField($phoneGroup, array('modifiable' => true));
     $phones->setValueTransformer($phonesTransformer);
     $this->add($phones);
     // email
     $emailsTransformer = new CollectionToGroupTransformer(array('em' => $em, 'fields' => array('email'), 'className' => 'Application\\ChiaBundle\\Entity\\Email', 'create_instance_callback' => function () use($contact, $em) {
         $email = new Email();
         $contact->addEmails($email);
         $email->setContact($contact);
         $em->persist($email);
         return $email;
     }, 'remove_instance_callback' => function ($email) use($contact, $em) {
         $contact->getEmails()->removeElement($email);
         $em->remove($email);
     }));
     $emailGroup = new FieldGroup('emails');
     $emailGroup->add(new TextField('email'));
     $emails = new CollectionField($emailGroup, array('modifiable' => true));
     $emails->setValueTransformer($emailsTransformer);
     $this->add($emails);
     // address
     $addressesTransformer = new CollectionToGroupTransformer(array('em' => $em, 'fields' => array('address', 'city', 'state', 'country', 'postal_code'), 'className' => 'Application\\ChiaBundle\\Entity\\Address', 'create_instance_callback' => function () use($contact, $em) {
         $address = new Address();
         $contact->addAddresses($address);
         $address->setContact($contact);
         $em->persist($address);
         return $address;
     }, 'remove_instance_callback' => function ($address) use($contact, $em) {
         $contact->getAddresses()->removeElement($address);
         $em->remove($address);
     }));
     $addressGroup = new FieldGroup('addresses');
     $addressGroup->add(new TextareaField('address'));
     $addressGroup->add(new TextField('city'));
     $addressGroup->add(new TextField('state'));
     $addressGroup->add(new CountryField('country'));
     $addressGroup->add(new TextField('postal_code'));
     $addresses = new CollectionField($addressGroup, array('modifiable' => true));
     $addresses->setValueTransformer($addressesTransformer);
     $this->add($addresses);
 }
Beispiel #5
0
 public function testSetGenerator_calledAfterAdding()
 {
     $generator = $this->getMock('Symfony\\Component\\Form\\HtmlGeneratorInterface');
     $field = $this->createMockField('firstName');
     $field->expects($this->exactly(2))->method('setGenerator');
     $group = new FieldGroup('author');
     $group->add($field);
     $group->setGenerator($generator);
 }