2

I'm trying to add a password repeat using easy admin bundle but I'm not quite sure how to do it. I have these two properties in my entity

/**
 * @var string
 *
 * @Assert\NotBlank()
 * @Assert\Length(max="4096")
 */
private $plainPassword;

/**
 * @var string
 *
 * @ORM\Column(type="string", length=64)
 */
private $password;

If I add a type: repeated in my config.yml file it just creates two input fields but not a Password type. I believe form should be like this.

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('email', EmailType::class)
        ->add('username', TextType::class)
        ->add('plainPassword', RepeatedType::class, array(
            'type' => PasswordType::class,
            'first_options'  => array('label' => 'Password'),
            'second_options' => array('label' => 'Repeat Password'),
            )
        )
        ->add('termsAccepted', CheckboxType::class, array(
            'mapped' => false,
            'constraints' => new IsTrue(),
        )
    );
}

I have checked the easy admin bundle documentation but I'm kinda lost how to implement it. https://github.com/javiereguiluz/EasyAdminBundle/blob/master/Resources/doc/book/7-complex-dynamic-backends.md

Thanks

  • Edit Okay so I extended the AdminController of JavierEguiluz\Bundle\EasyAdminBundle\Controller\AdminController

    public function createNewForm($entity, array $entityProperties)
    {
        $userForm = parent::createNewForm($entity, $entityProperties);
    
        if ($entity instanceof User) {
            $userForm->remove('password');
            $userForm->add('plainPassword', RepeatedType::class, array(
                'type' => PasswordType::class,
                'first_options' => array('label' => 'Password'),
                'second_options' => array('label' => 'Re-enter Password')
            ));
        }
    
        return $userForm;
    }
    

But now when I try to insert / submit the form, sql error password cannot be null.

yceruto
  • 9,230
  • 5
  • 38
  • 65
dashred
  • 131
  • 1
  • 12

2 Answers2

2

Try with type Symfony\Component\Form\Extension\Core\Type\PasswordType instead of password.

        form:
            fields:
                - {'property': 'plainPassword', type: 'repeated', type_options: { type: Symfony\Component\Form\Extension\Core\Type\PasswordType, required: false, first_options: {label: 'label.password'}, second_options: {label: 'label.password_confirmation'} } }
bodrak
  • 46
  • 3
1

You can set this in the config.yml that map your entity, for example I have this:

easy_admin:
entities:
    Usuario:
        class: AppBundle\Entity\Usuario
        controller: AppBundle\Controller\UsuarioController
        form:
            fields:
                - 'documento'
                - 'codigo'
                - 'nombre'
                - 'apellido'
                - 'email'
                - { property: 'passwordEnClaro', type: 'repeated', type_options: { type: 'password', invalid_message: 'Las dos contraseñas deben coincidir', first_options: { label: 'Contraseña' }, second_options: { label: 'Confirmar Contraseña' }, required: false } }
                - { property: 'rol', type: 'choice', type_options: { choices:  { 'ROLE_ADMIN': 'ROLE_ADMIN', 'ROLE_FUNCIONARIO': 'ROLE_FUNCIONARIO', 'ROLE_DOCENTE': 'ROLE_DOCENTE', 'ROLE_ESTUDIANTE': 'ROLE_ESTUDIANTE' }, attr: { 'data-widget': 'select2' } } }
                - { property: 'dependencia', type: 'easyadmin_autocomplete', type_options: { class: 'AppBundle\Entity\Dependencia' } }

Note: The controller definition is only for PreUpdate use.

Andres
  • 11
  • 1
  • property 'passwordEnClaro' configuration works for symfony 2.8.* and in order to make work in symfony 3.2.* work `- { property: 'plainPassword', type: 'password', label: 'Password', help: 'Passwords must have at least 8 characters', type_options: { required: false } }` – Rodolfo Velasco May 17 '17 at 16:39
  • there should be some issue since using symfony 4 beta 1 it says Could not load type "password" – Leggy7 Nov 04 '17 at 15:26
  • bodrak answer has the solution for symfony 4 (type: Symfony\Component\Form\Extension\Core\Type\PasswordType) – Razvan.432 Apr 23 '19 at 06:39