36 lines
920 B
PHP
36 lines
920 B
PHP
<?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(),
|
|
)
|
|
)
|
|
);
|
|
}
|
|
}
|
|
}
|