deblan.tv/vendor/trinity/src/Trinity/.svn/pristine/bf/bf5f9274394ee9787b2c3d1294185af152b52ec9.svn-base
2015-03-02 21:57:49 +01:00

82 lines
2 KiB
Plaintext

<?php
namespace Trinity\Component\Form\EventListener;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class FileDeleteFormListener implements EventSubscriberInterface
{
/**
*
* @var FormFactoryInterface
*
*/
protected $factory;
/**
*
* @var array
*
*/
protected $fields = array();
/**
* @param \Symfony\Component\Form\FormFactoryInterface $factory
* @param array $fields
*/
public function __construct(FormFactoryInterface $factory, array $fields)
{
$this->factory = $factory;
$this->fields = $fields;
}
/**
* {@inheritDoc}
*/
public static function getSubscribedEvents()
{
return array(
FormEvents::PRE_SUBMIT => 'preSubmit',
);
}
/**
* @param \Symfony\Component\Form\Event\DataEvent $event
*
* @return mixed
* @throws \Symfony\Component\Form\Exception\UnexpectedTypeException
*/
public function preSubmit(FormEvent $event)
{
$form = $event->getForm();
$data = $event->getData();
if (null === $data || '' === $data) {
$data = array();
}
if (!is_array($data) && !$data instanceof \Traversable) {
throw new UnexpectedTypeException($data, 'array or \Traversable');
}
foreach ($this->fields as $k => $v) {
$form->add(
$this->factory->createNamed(
sprintf('delete_%s', $v),
'checkbox',
null,
array(
'mapped' => true,
'required' => false,
'auto_initialize' => false,
)
)
);
}
}
}