Wallogit.com
2017 © Pedro Peláez
Bundle to allow you manipulate subforms in symfony 2 easilly
Bundle for Symfony 2., (*1)
This Bundle allow you to manage multiple subforms with translations on the fields name the easy way., (*2)
{
"require": {
...,
"loamok/subformsmadeeasy" : "dev-master"
}
}
public function registerBundles() {
$bundles = array(
...,
new Loamok\SubformsMadeEasyBundle\LoamokSubformsMadeEasyBundle(),
);
This bundle require Jquery 1.11.1 or compatible., (*3)
This bundle is usable with assetic., (*4)
Make sure you're correctly configure assetic to run in your bundle :, (*5)
app/config/config.yml :, (*6)
# Assetic Configuration
assetic:
bundles:
...
- YourWonderfullBundle
Add Jquery to your layout.twig :, (*7)
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Add the ressource to your layout.twig :, (*8)
{% javascripts '@LoamokSubformsMadeEasyBundle/Resources/public/js/dynamicSubforms.js' %}
<script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}
Configure an Entity to use another entity in a multiple relation :, (*9)
<?php
namespace Your\WonderfullBundle\Entity;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* Article
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="ArticleRepository")
* @ORM\HasLifecycleCallbacks
*/
class Article {
/**
* @var integer ... */
private $id;
/**
* @var Collection
*
* @ORM\ManyToMany(targetEntity="Media", inversedBy="articles", cascade={"persist"})
*/
private $medias;
Add subforms to the form associate :, (*10)
<?php
namespace Your\WonderfullBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class ArticleType extends AbstractType {
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add(...)
->add('medias', 'collection', array(
'type' => new MediaType(),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false
))
Add the row in your form.html.twig :, (*11)
{{ 'admin.article.form.title' | trans }}
< div class="well"> {{ form_start(form, {'attr': {'class': 'form-horizontal'}}) }} ...{{ form_label(form.medias, 'article.form.medias'|trans, {'label_attr': {'class': 'col-sm-3 control-label'}}) }} {{ form_errors(form.medias) }}{{ form_widget(form.medias, {'attr': {'class': 'form-control', 'style': 'height: auto;'}}) }}
In the same twig template but at the end of the file add the js configuration :, (*12)
Note: Samples with a relation beetween post, tags, notes), (*13)
Note: You can use translations for the labels by using "{{ 'a.trans.key' | trans }}" in place of the label, (*14)
Note: The three following samples will do exactly the same result, (*15)
Minimalistic config :, (*16)
Short config :, (*17)
Long config :, (*18)
Adding two subforms to a form :, (*19)
This bundle is under the GNU LESSER GENERAL PUBLIC LICENSE Version 3., (*20)
See the complete license in the bundle:, (*21)
LICENSE