Author: Fabien Potencier (fabien.potencier@symfony-project.com)
Inheritance: extends Field, implements IteratorAggregate, implements Symfony\Component\Form\FieldGroupInterface
Example #1
0
 /**
  * {@inheritDoc}
  */
 public function getAttributes()
 {
     if ($this->isField()) {
         return array_merge(parent::getAttributes(), array('size' => '1'));
     }
     return parent::getAttributes();
 }
Example #2
0
 /**
  * {@inheritDoc}
  */
 public function bind($data)
 {
     if ($this->mode === self::GROUP) {
         parent::bind($data);
     } else {
         Field::bind($data);
     }
 }
Example #3
0
 /**
  * {@inheritDoc}
  */
 protected function configure()
 {
     $this->addRequiredOption('secret');
     $this->addOption('tmp_dir', sys_get_temp_dir());
     parent::configure();
     $this->add(new Field('file'));
     $this->add(new HiddenField('token'));
     $this->add(new HiddenField('original_name'));
 }
Example #4
0
 public function bind($taintedData)
 {
     if (is_null($taintedData)) {
         $taintedData = array();
     }
     foreach ($this as $name => $field) {
         if (!isset($taintedData[$name]) && $this->getOption('modifiable') && $name != '$$key$$') {
             $this->remove($name);
         }
     }
     foreach ($taintedData as $name => $value) {
         if (!isset($this[$name]) && $this->getOption('modifiable')) {
             $this->add($this->newField($name, $name));
         }
     }
     return parent::bind($taintedData);
 }
Example #5
0
 protected function updateObject(&$objectOrArray)
 {
     parent::updateObject($objectOrArray);
     foreach ($this->removedFields as $name) {
         unset($objectOrArray[$name]);
     }
 }
Example #6
0
 /**
  * {@inheritDoc}
  */
 protected function reverseTransform($value)
 {
     return parent::reverseTransform(array_merge($value['date'], $value['time']));
 }
 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());
 }
Example #8
0
 /**
  * Binds the form with the given data.
  *
  * @param  array $taintedData  The data to bind to the form
  * @return boolean             Whether the form is valid
  */
 protected function doBind(array $taintedData)
 {
     parent::bind($taintedData);
 }
 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);
 }
Example #10
0
 /**
  * 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;
 }
Example #11
0
 /**
  * Expose method for testing purposes
  */
 public function getNormalizedData()
 {
     return parent::getNormalizedData();
 }
Example #12
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;
 }
Example #13
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);
 }