From e80fb2abbcdd09b05c8f1f8795154567839eeb7f Mon Sep 17 00:00:00 2001 From: european Date: Fri, 4 Oct 2013 15:24:05 +0200 Subject: [PATCH 1/4] Add Fieldgroup Support --- Configuration/FieldGroup.php | 2 +- Driver/AnnotationsDriver.php | 10 ++++++++-- FormMapper.php | 16 ++++++++++++++-- FormMetadata.php | 26 ++++++++++++++++++++++++-- 4 files changed, 47 insertions(+), 7 deletions(-) diff --git a/Configuration/FieldGroup.php b/Configuration/FieldGroup.php index bfa1f38..2a47ede 100644 --- a/Configuration/FieldGroup.php +++ b/Configuration/FieldGroup.php @@ -6,7 +6,7 @@ /** * e.g. @Form\FieldGroup('example') - * @author camm (camm@flintinteractive.com.au) + * @author camm (camm@flintinteractive.com.au), european(info@nils-werner.com) */ class FieldGroup { diff --git a/Driver/AnnotationsDriver.php b/Driver/AnnotationsDriver.php index 9078bf9..bc2b2fc 100644 --- a/Driver/AnnotationsDriver.php +++ b/Driver/AnnotationsDriver.php @@ -11,7 +11,7 @@ use \FlintLabs\Bundle\FormMetadataBundle\FormMetadata; /** * - * @author camm (camm@flintinteractive.com.au) + * @author camm (camm@flintinteractive.com.au), european(info@nils-werner.com) */ class AnnotationsDriver implements MetadataDriverInterface { @@ -32,7 +32,13 @@ public function getMetadata($entity) $properties = $reflectionClass->getProperties(); foreach($properties as $property) { $field = $reader->getPropertyAnnotation($property, 'FlintLabs\Bundle\FormMetadataBundle\Configuration\Field'); - if(!empty($field)) { + $fieldGroup = $reader->getPropertyAnnotation($property, 'FlintLabs\Bundle\FormMetadataBundle\Configuration\FieldGroup'); + if (!empty($fieldGroup) && !empty($field)) { + if (empty($field->name)) { + $field->name = $property->getName(); + } + $metadata->addGroup($fieldGroup, $field); + } elseif(!empty($field)) { if(empty($field->name)) { $field->name = $property->getName(); } diff --git a/FormMapper.php b/FormMapper.php index 9dc8560..ce29bba 100644 --- a/FormMapper.php +++ b/FormMapper.php @@ -14,8 +14,7 @@ /** * Obtains any metadata from the entity and adds it's configuration * to the form - * TODO: Support field groups - * @author camm (camm@flintinteractive.com.au) + * @author camm (camm@flintinteractive.com.au), european(info@nils-werner.com) */ class FormMapper { @@ -67,6 +66,19 @@ public function createFormBuilder($entity, $data = null, array $options = array( // TODO: Detect references to "%service.id%" for service constructor dependency $formBuilder->add($field->name, $field->value, $field->options); } + + $groups = $metadata->getGroups(); + + foreach($groups as $groupName => $fields) { + $builder = $formBuilder->create($groupName, 'form', array('virtual' => true)); + + + foreach($fields as $field) { + $builder->add($field->name, $field->value, $field->options); + } + + $formBuilder->add($builder); + } return $formBuilder; } diff --git a/FormMetadata.php b/FormMetadata.php index 3b74a4e..b26e5a8 100644 --- a/FormMetadata.php +++ b/FormMetadata.php @@ -11,7 +11,7 @@ use FlintLabs\Bundle\FormMetadataBundle\Configuration\Field; /** * The meta data containing the configuration of the form - * @author camm (camm@flintinteractive.com.au) + * @author camm (camm@flintinteractive.com.au), european(info@nils-werner.com) */ class FormMetadata { @@ -21,7 +21,6 @@ class FormMetadata protected $fields = array(); /** - * TODO: Add in support for field groups * @var array */ protected $groups = array(); @@ -43,5 +42,28 @@ public function getFields() { return $this->fields; } + + /** + * + * @param FieldGroup $fieldGroup + * @param Field $field + */ + public function addGroup(FieldGroup $fieldGroup, Field $field) + { + if(!empty($this->groups[$fieldGroup->value])) { + $this->groups[$fieldGroup->value][] = $field; + } else { + $this->groups[$fieldGroup->value][] = $field; + } + } + + /** + * + * @return array + */ + public function getGroups() + { + return $this->groups; + } } From e5cf5586008e4d1fedcb6c88f1d0d29cd8705cf3 Mon Sep 17 00:00:00 2001 From: nwerner Date: Fri, 4 Oct 2013 15:26:42 +0200 Subject: [PATCH 2/4] Add @Annotation --- Configuration/Field.php | 5 +++-- Configuration/FieldGroup.php | 6 ++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Configuration/Field.php b/Configuration/Field.php index 95135b1..e4a4e8a 100644 --- a/Configuration/Field.php +++ b/Configuration/Field.php @@ -8,15 +8,16 @@ * file that was distributed with this source code. */ namespace FlintLabs\Bundle\FormMetadataBundle\Configuration; - +use \Doctrine\Common\Annotations\Annotation; /** * Contains the configuration elements for the field * * e.g. @Form\Field("text", foo="bar") * * @author camm (camm@flintinteractive.com.au) + * @Annotation */ -class Field extends \Doctrine\Common\Annotations\Annotation +class Field extends Annotation { /** diff --git a/Configuration/FieldGroup.php b/Configuration/FieldGroup.php index 2a47ede..dc45f1e 100644 --- a/Configuration/FieldGroup.php +++ b/Configuration/FieldGroup.php @@ -3,12 +3,14 @@ * Copyright Cameron Manderson (c) 2011 All rights reserved. * Date: 21/09/11 */ - +namespace FlintLabs\Bundle\FormMetadataBundle\Configuration; +use \Doctrine\Common\Annotations\Annotation; /** * e.g. @Form\FieldGroup('example') * @author camm (camm@flintinteractive.com.au), european(info@nils-werner.com) + * @Annotation */ -class FieldGroup +class FieldGroup extends Annotation { /** * @var From b30fb32693cbd575d87d543cf718e2565567dda0 Mon Sep 17 00:00:00 2001 From: nwerner Date: Fri, 4 Oct 2013 15:30:17 +0200 Subject: [PATCH 3/4] add group example --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index b8ddce1..09a255e 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,13 @@ as a best practice. * @Form\Field("date", widget="single_text") */ +**Group Examlpe** + + /** + * @Form\Field("date", widget="single_text") + * @Form\FieldGroup("example") + */ + ### Entity with some basic form annotations use FlintLabs\Bundle\FormMetadataBundle\Configuration as Form; From 29c7a9247e642c4d74c29545929d470c681e8ab6 Mon Sep 17 00:00:00 2001 From: nwerner Date: Fri, 4 Oct 2013 15:38:46 +0200 Subject: [PATCH 4/4] Changed the createFormBuilder method --- FormMapper.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/FormMapper.php b/FormMapper.php index ce29bba..2b68cef 100644 --- a/FormMapper.php +++ b/FormMapper.php @@ -43,7 +43,7 @@ public function __construct(FormFactory $factory) * @param $form * @return */ - public function createFormBuilder($entity, $data = null, array $options = array()) + public function createFormBuilder($data = null, array $options = array()) { // Build the $form $formBuilder = $this->factory->createBuilder('form', $data, $options); @@ -53,7 +53,7 @@ public function createFormBuilder($entity, $data = null, array $options = array( // Look to the readers to find metadata foreach ($this->drivers as $driver) { - $metadata = $driver->getMetadata($entity); + $metadata = $driver->getMetadata($data); if(!empty($metadata)) break; }