Created
March 19, 2014 13:54
-
-
Save florinutz/9642120 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// florin 3/13/14 12:12 PM | |
namespace Wyzbiz\Bundle\MainBundle\Form\Type; | |
use Sonata\AdminBundle\Form\DataTransformer\ModelsToArrayTransformer; | |
use Symfony\Component\DependencyInjection\ContainerAwareInterface; | |
use Symfony\Component\DependencyInjection\ContainerInterface; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\FormBuilder; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\Form\FormEvent; | |
use Symfony\Component\Form\FormEvents; | |
use Symfony\Component\Form\FormInterface; | |
use Symfony\Component\OptionsResolver\OptionsResolverInterface; | |
use Wyzbiz\Bundle\MainBundle\Entity\Customer; | |
use Wyzbiz\Bundle\MainBundle\Entity\InvoicingClient; | |
use Wyzbiz\Bundle\MainBundle\Form\Transformer\CustomerSearchToIdTransformer; | |
class PlatformsCustomersType extends AbstractType implements ContainerAwareInterface | |
{ | |
/** @var ContainerInterface */ | |
private $container; | |
public function getParent() | |
{ | |
return 'form'; | |
} | |
public function getName() | |
{ | |
return 'wyz_platforms_customers'; | |
} | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
$builder | |
->add('customer', 'text', ['label' => 'Search Customer']) | |
->add('platform', 'entity', [ | |
'class' => 'WyzbizMainBundle:Platform', | |
'empty_value' => 'All', | |
'label' => 'Choose platform' | |
]) | |
->add('customers', 'entity', [ | |
'class' => 'WyzbizMainBundle:Customer', | |
'property' => 'username', | |
'choices' => [], | |
'multiple' => true, | |
'expanded' => true, | |
]) | |
; | |
$formModifier = function(FormInterface $form, InvoicingClient $invoicingClient = null) { | |
$customers = $invoicingClient ? $invoicingClient->getCustomers() : []; | |
$form->add('customers', 'entity', [ | |
'class' => 'WyzbizMainBundle:Customer', | |
'property' => 'username', | |
'choices' => $customers, | |
'multiple' => true, | |
'expanded' => true, | |
]); | |
}; | |
$builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) use ($formModifier) { | |
$form = $event->getForm(); | |
$parentForm = $form->getParent(); | |
if (($invoicingClient = $parentForm->getData()) && $invoicingClient->getId()) { | |
$formModifier($form, $invoicingClient); | |
} | |
}); | |
$builder->get('customers')->addEventListener(FormEvents::POST_SUBMIT, function(FormEvent $event) use ($formModifier) { | |
$form = $event->getForm()->getParent(); | |
$parentForm = $event->getForm()->getParent()->getParent(); | |
$invoicingClient = $parentForm->getData(); | |
$formModifier($form, $invoicingClient); | |
}); | |
} | |
public function setContainer(ContainerInterface $container = null) | |
{ | |
$this->container = $container; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment