trinity-cms-bundles/src/Trinity/Bundle/UserBundle/Form/EventListener/AddCodeFieldSubscriber.php

36 lines
920 B
PHP
Raw Normal View History

2015-03-03 18:51:20 +01:00
<?php
namespace Trinity\Bundle\UserBundle\Form\EventListener;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Validator\Constraints\NotBlank;
class AddCodeFieldSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return array(FormEvents::PRE_SET_DATA => 'preSetData');
}
public function preSetData(FormEvent $event)
{
$data = $event->getData();
$form = $event->getForm();
if (!$data || !trim($data->getCode())) {
$form->add(
'code',
'text',
array(
'required' => true,
'constraints' => array(
new NotBlank(),
)
)
);
}
}
2015-03-05 17:51:23 +01:00
}