Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Password
<?php
//UserAdmin Form
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('email', 'email', array('label' => 'form.email', 'translation_domain' => 'FOSUserBundle'))
->add('username', null, array('label' => 'form.username', 'translation_domain' => 'FOSUserBundle'))
->add('firstName', null, array('label' => 'form.firstname'))
->add('lastName', null, array('label' => 'form.lastname'))
->add('address1', null, array('label' => 'form.fax'))
->add('city', null, array('label' => 'form.city'))
->add('state', null, array('label' => 'form.state'))
->add('zipcode', null, array('label' => 'form.zipcode'))
->add('primaryPhone', null, array('label' => 'form.primaryPhone'))
->add('altPhone', null, array('label' => 'form.altPhone'))
;
$builder->addEventSubscriber(new PasswordFieldSubscriber());
}
//Subscriber
namespace FA\BackEnd\UserAdminBundle\Form\EventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
class PasswordFieldSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return array(FormEvents::PRE_SET_DATA => 'preSetPassword');
}
public function preSetPassword(FormEvent $event)
{
$user = $event->getData();
$form = $event->getForm();
$required = true;
if ($user->getId()) {
$required = false;
}
$form->add('plainPassword', 'repeated', array(
'type' => 'password',
'options' => array('translation_domain' => 'FOSUserBundle'),
'required' => $required,
'first_options' => array('label' => 'form.password'),
'second_options' => array('label' => 'form.password_confirmation'),
'invalid_message' => 'fos_user.password.mismatch',
));
return $required;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment