Skip to content

Instantly share code, notes, and snippets.

@ahsio
Created June 4, 2012 10:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahsio/2867617 to your computer and use it in GitHub Desktop.
Save ahsio/2867617 to your computer and use it in GitHub Desktop.
array
'search_criteria_1' =>
array
'name' => string 'Search Criteria 1' (length=17)
'choices' =>
array
0 =>
array
'element' => string 'Element 1' (length=9)
'code' => string '001' (length=3)
'is_selected' => boolean false
1 =>
array
'element' => string 'Element 2' (length=9)
'code' => string '002' (length=3)
'is_selected' => boolean false
2 =>
array
'element' => string 'Element 3' (length=9)
'code' => string '003' (length=3)
'is_selected' => boolean false
'search_criteria_2' =>
array
'name' => string 'Search Criteria 2' (length=17)
'choices' =>
array
0 =>
array
'element' => string 'Element 1' (length=9)
'code' => string '101' (length=3)
'is_selected' => boolean false
'choices' =>
array
0 =>
array
'element' => string 'Element 11' (length=10)
'code' => string '111' (length=3)
'is_selected' => boolean false
1 =>
array
'element' => string 'Element 12' (length=10)
'code' => string '112' (length=3)
'is_selected' => boolean false
...
<?php
namespace Examplecms\Bundle\ExampleCoreBundle\Form\EventListener;
use Examplecms\Bundle\ExampleCoreBundle\Form\GroupTreeType;
use Symfony\Component\Form\Event\DataEvent,
Symfony\Component\Form\FormEvents,
Symfony\Component\Form\FormFactoryInterface,
Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Part of the Search Form Type.
*/
class GroupTreeSubscriber implements EventSubscriberInterface
{
/**
* @var FormFactoryInterface $factory
*/
protected $factory;
/**
* @var array $options
*/
protected $options;
/**
* The main constructor.
*
* @param FormFactoryInterface $factory The form factory
* @param array $options The options
*/
public function __construct(FormFactoryInterface $factory, array $options = array())
{
$defaults = array('searchCriteria' => null);
$this->factory = $factory;
$this->options = array_merge($defaults, $options);
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return array(FormEvents::PRE_SET_DATA => 'preSetData');
}
/**
* Pre Set Data.
*
* @param DataEvent $event The triggered event.
*
* @return type
*/
public function preSetData(DataEvent $event)
{
$data = $event->getData();
$form = $event->getForm();
if (null === $data) {
return;
}
if (isset($data['name'])) {
$form->add($this->factory->createNamed('hidden', 'name', null, array(
'label' => $data['name'],
)));
}
if (isset($data['code']) && !isset($data['is_selected'])) {
$data['is_selected'] = false;
}
if (isset($data['is_selected'])) {
$form->add($this->factory->createNamed('checkbox', 'is_selected', null, array(
'label' => $data['element'],
'required' => false,
)));
}
if (isset($data['choices']) && null !== $data['choices']) {
$form->add($this->factory->createNamed('collection', 'choices', null, array(
'type' => new GroupTreeType(),
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'by_reference' => false,
'label' => false,
)));
}
}
}
<?php
namespace Examplecms\Bundle\ExampleCoreBundle\Form;
use Examplecms\Bundle\ExampleCoreBundle\Form\EventListener\GroupTreeSubscriber;
use Symfony\Component\Form\AbstractType,
Symfony\Component\Form\FormBuilder;
/**
* A Search Element Type.
*/
class GroupTreeType extends AbstractType
{
/**
* Setup the form.
*
* @param FormBuilder $builder The main form builder.
* @param array $options Options for the build
*/
public function buildForm(FormBuilder $builder, array $options)
{
$subscriber = new GroupTreeSubscriber(
$builder->getFormFactory(),
array()
);
$builder->addEventSubscriber($subscriber);
}
/**
* @param array $options
*
* @return array
*/
public function getDefaultOptions(array $options)
{
return array();
}
/**
* @return string
*/
public function getName()
{
return 'group_tree';
}
}
<?php
namespace Examplecms\Bundle\ExampleCoreBundle\Form\EventListener;
use Examplecms\Bundle\ExampleCoreBundle\Form\SearchCriteriaType;
use Symfony\Component\Form\Event\DataEvent,
Symfony\Component\Form\FormEvents,
Symfony\Component\Form\FormFactoryInterface,
Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Search Criteria Subscriber
*/
class SearchCriteriaSubscriber implements EventSubscriberInterface
{
/**
* @var FormFactoryInterface
*/
protected $factory;
/**
* @var array
*/
protected $options;
/**
* Main constructor.
*
* @param FormFactoryInterface $factory The form
* @param array $options The options
*/
public function __construct(FormFactoryInterface $factory, array $options = array())
{
$defaults = array('searchCriteria' => null);
$this->factory = $factory;
$this->options = array_merge($defaults, $options);
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return array(FormEvents::PRE_SET_DATA => 'preSetData');
}
/**
* Set data for the form.
*
* @param DataEvent $event
*/
public function preSetData(DataEvent $event)
{
$data = $event->getData();
$form = $event->getForm();
if (null === $data) {
return;
}
if (null === $data[$this->options['searchCriteria']]) {
return;
}
$form->add($this->factory->createNamed(new searchCriteriaType(), $this->options['searchCriteria'], null, array()));
}
}
<?php
namespace Examplecms\Bundle\ExampleCoreBundle\Form;
use Examplecms\Bundle\ExampleCoreBundle\Form\EventListener\GroupTreeSubscriber;
use Symfony\Component\Form\AbstractType,
Symfony\Component\Form\FormBuilder;
use Examplecms\Bundle\ExampleCoreBundle\Form\EventListener\SearchSubscriber;
/**
* Main search criteria Type.
*/
class SearchCriteriaType extends AbstractType
{
/**
* Setup the form.
*
* @param FormBuilder $builder The main form builder.
* @param array $options Options for the build
*/
public function buildForm(FormBuilder $builder, array $options)
{
$subscriber = new GroupTreeSubscriber(
$builder->getFormFactory(),
array()
);
$builder->addEventSubscriber($subscriber);
}
/**
* @param array $options
*
* @return array
*/
public function getDefaultOptions(array $options)
{
return array();
}
/**
* @return string
*/
public function getName()
{
return 'search_criteria';
}
}
<?php
namespace Examplecms\Bundle\ExampleCoreBundle\Form;
use Symfony\Component\Form\AbstractType,
Symfony\Component\Form\FormBuilder;
use Examplecms\Bundle\ExampleCoreBundle\Form\EventListener\SearchCriteriaSubscriber,
Examplecms\Bundle\ExampleCoreBundle\Validator\Constraints as ExamplecmsAssert;
/**
* Search form.
*/
class SearchType extends AbstractType
{
/**
* @param FormBuilder $builder The builder object
* @param array $options Its options
*/
public function buildForm(FormBuilder $builder, array $options)
{
// Take into account search_criteria_1
if ($options['search_criteria_1']) {
$subscriber = new SearchCriteriaSubscriber(
$builder->getFormFactory(),
array('searchCriteria' => 'search_criteria_1')
);
$builder->addEventSubscriber($subscriber);
}
// Take into account search_criteria_2
if ($options['search_criteria_2']) {
$subscriber = new SearchCriteriaSubscriber(
$builder->getFormFactory(),
array('searchCriteria' => 'search_criteria_2')
);
$builder->addEventSubscriber($subscriber);
}
// Take into account search_criteria_3
if ($options['search_criteria_3']) {
$subscriber = new SearchCriteriaSubscriber(
$builder->getFormFactory(),
array('searchCriteria' => 'search_criteria_3')
);
$builder->addEventSubscriber($subscriber);
}
// Take into account search_criteria_4
if ($options['search_criteria_4']) {
$subscriber = new SearchCriteriaSubscriber(
$builder->getFormFactory(),
array('searchCriteria' => 'search_criteria_4')
);
$builder->addEventSubscriber($subscriber);
}
// ...
}
}
/**
* @param array $options
*
* @return array
*/
public function getDefaultOptions(array $options)
{
return array(
'search_criteria_1' => false,
'search_criteria_2' => false,
'search_criteria_3' => false,
'search_criteria_4' => false,
'search_criteria_5' => false,
'search_criteria_6' => false,
'search_criteria_7' => false,
'search_criteria_8' => false,
'search_criteria_9' => false,
// ...
'validation_constraint' => new ExamplecmsAssert\Search(),
);
}
/**
* @return string The name of the form
*/
public function getName()
{
return 'search';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment