Skip to content

Instantly share code, notes, and snippets.

@florinutz
Created March 19, 2014 09:22
Show Gist options
  • Save florinutz/9638305 to your computer and use it in GitHub Desktop.
Save florinutz/9638305 to your computer and use it in GitHub Desktop.
<?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('platform', 'entity', ['class' => 'Wyzbiz\Bundle\MainBundle\Entity\Platform', 'empty_value' => 'All'])
->add('customer', 'text')
;
$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) {
$customer = $event->getData();
$form = $event->getForm();
if (!$customer || null === $customer->getId()) {
$formModifier($form);
}
});
/*$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function(FormEvent $event) use ($formModifier) {
$data = $event->getData();
$formModifier($event->getForm(), $data->getInvoicingClient());
}
);
$builder->get('customer')->addEventListener(
FormEvents::POST_SUBMIT,
function(FormEvent $event) use ($formModifier) {
$invoicingClient = $event->getForm()->getData();
$formModifier($event->getForm()->getParent(), $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