dev-master
9999999-devThe Dynamic Form Bundle
MIT
The Requires
- php >=5.3.3
- symfony/symfony >=2.1
- twig/twig >=1.8,<2.0-dev
- doctrine/common >=2.1,<2.4.x-dev
- doctrine/doctrine-fixtures-bundle >=2.1
The Development Requires
by Aymen Bouchekoua
The Dynamic Form Bundle
A Dynamic Form Builder for Symfony2 using kelp404/angular-form-builder., (*2)
Add the following lines in your composer.json: ``` json // composer.json { "require": { "talan/dynamic-form" : "dev-master" } }, (*3)
### Initialize the bundle To start using the bundle, register the bundle in your application's kernel class: ``` php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new Talan\Bundle\DynamicFormBundle\TalanDynamicFormBundle(), ); )
Execute the following commands under the project's main folder:, (*4)
$ php app/console doctrine:schema:update --force $ php app/console doctrine:fixture:load --fixtures=vendor/talan/dynamic-form/Talan/Bundle/DynamicFormBundle/DataFixtures/ORM --append
This will create 8 tables prefixed with "talan_" in your database schema:, (*5)
TalanDynamicFormBundle comes with default controllers and views that uses the developed services to offer the Back and Front Office functionnalities. To activate the required pages you simply need to import the bundle's routing file:, (*6)
``` yml, (*7)
talan_dynamic_form: resource: "@TalanDynamicFormBundle/Resources/config/routing.yml" prefix: /, (*8)
You may of course add a prefix of your choice. If you like to add specific behaviors to the controllers or change the displayed pages, you simply extends this bundle and make your own implementations of the controllers or the twigs of this bundle. #### Override the default layout.html.twig The layout file of this bundle imports the javascript dependencies from various links. Therefore, we highly recommande that you download the required frameworks mentioned above, override the layout file and use the assetics to include the downloaded frameworks. The easiest way to override a bundle's template is to simply place a new one in your app/Resources folder. To override the layout template located at Resources/views/layout.html.twig in the TalanDynamicFormBundle directory, you would place your new layout template at app/Resources/TalanDynamicFormBundle/views/layout.html.twig The following Twig template file is an example of a layout file that might be used to override the one provided by the bundle. ``` html+jinja {% extends 'AcmeDemoBundle::layout.html.twig' %} {% block title %}Acme Demo Application{% endblock %} {% block content %} {% block talan_dynamic_form_content %}{% endblock %} {% endblock %}
The main thing to note in this template is the block named talan_dynamic_form_content. This is the block where the content from each of the different bundle's actions will be displayed, so you must make sure to include this block in the layout file you will use to override the default one., (*9)
When a user of the application submit the values of a certain form created by this bundle, these values should be attached somehow to that specific user. However, this link between the submitted values and the user depends on the nature of the application. Therefore, it should be up to the developer and the Back-office to specify the way to attach these elements., (*10)
This bundle provides the above feature by introducing the ValueOwnerProviderInterface and abstract implementation AbstractOwnerProvider., (*11)
The ValueOwnerProviderInterface requires the implementation of 3 methods which are getValueOwner() , getOwnerListTemplate() and getValueOwnerList($formId). And we added the AbstractOwnerProvider Class to provide a default owner template., (*12)
So by implementing th interface or extending the abstract class then injecting the service with the required tag, the developer should be able to specify how the submitted values and the user could be linked to each other., (*13)
TalanDynamicFormBundle comes with 2 pre-implemented classes of the ValueOwnerProviderInterface. They are AbstractUserValueProvider and SessionValueProvider which is declared as a service., (*14)
Here is the ServiceValueProvider Class: ``` php class SessionValueProvider extends AbstractValueOwnerProvider { protected $session; protected $em;, (*15)
public function __construct(EntityManager $em,Session $session) { $this->session = $session; $this->em = $em; } /** * (non-PHPdoc) * @see \Talan\Bundle\DynamicFormBundle\Service\ValueOwnerProviderInterface::getValueOwner() */ public function getValueOwner() { return $this->session->getId(); }
}, (*16)
As you can see the only requiered method to implement is *getValueOwner()* and we injected the *session* variable in order to get the user's session ID. To activate the session value provider, you need to add the configuration below to your services.yml. ``` yml # src/Demo/Bundle/DemoBundle/Resources/config/services.yml parameters: talan_dynamic_form.session_provider.class: Talan\Bundle\DynamicFormBundle\Service\Impl\SessionValueProvider services: talan_dynamic_form.session_value_provider: class: "%talan_dynamic_form.session_provider.class%" arguments: ["@doctrine.orm.entity_manager","@session"] tags: - {name: talan_dynamic_form.value_owner_provider, alias: "Session Provider"}
Note that we injected the @doctrine.orm.entity_manager and @security as an argument of the SessionValueProvider and we tagged the service with talan_dynamic_form.value_owner_provider tag. Tagging this service is very important as it allows the bundle to recognize this class as a ValueOwnerProvider. As for the alias, it is the label that will be shown to the back-office to choose from when creating a Form., (*17)
Here is the AbstractUserValueProvider Class: ``` php abstract class AbstractUserValueProvider extends AbstractValueOwnerProvider { protected $security; protected $em;, (*18)
public function __construct(EntityManager $em,SecurityContext $security) { $this->security = $security; $this->em = $em; } /** * (non-PHPdoc) * @see \Talan\Bundle\DynamicFormBundle\Service\ValueOwnerProviderInterface::getValueOwner() */ public function getValueOwner() { if (!$this->security) { throw new \LogicException('The SecurityBundle is not registered in your application.'); } if (null === $token = $this->security->getToken()) { return; } if (!is_object($user = $token->getUser())) { return; } return $user; }
}, (*19)
This class is declared abstract so that each user can define its own implemtation of the two methods *getValueOwnerList()* and *getOwnerListTemplate()*. Here is a sample implementation: ``` php class UserValueProvider extends AbstractUserValueProvider { protected $security; protected $em; public function __construct(EntityManager $em, SecurityContext $security) { $this->security = $security; $this->em = $em; } public function getOwnerListTemplate() { return 'TalanUserBundle::connectedUser.html.twig'; } public function getValueOwnerList($formId){ return $this->em->getRepository('TalanUserBundle:User')->findUsersByForm($formId); } }
And here is the correspondent owner list template connectedUser.html.twig:, (*20)
{% extends 'TalanDynamicFormBundle:OwnerList:default.html.twig' %} {% block talan_dynamic_form_owner_table %}
The Dynamic Form Bundle
MIT