<?php
namespace App\Form\Subscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
class UserSubscriber implements EventSubscriberInterface
{
/**
* @inheritDoc
*/
public static function getSubscribedEvents()
{
return [FormEvents::PRE_SET_DATA => 'onPreSetData'];
}
/**
* Adds password fields to form only when creating a new user
*
* @param FormEvent $event
*/
public function onPreSetData(FormEvent $event)
{
$user = $event->getData();
if (null === $user->getId()) {
$form = $event->getForm();
$form->add('password', RepeatedType::class, [
'type' => PasswordType::class,
'first_options' => ['label' => 'Password'],
'second_options' => ['label' => 'Repeat Password'],
]);
}
}
}