Symfony 如何在easy admin bundle中添加重复类型字段

Symfony 如何在easy admin bundle中添加重复类型字段,symfony,symfony2-easyadmin,Symfony,Symfony2 Easyadmin,我正在尝试使用easy admin bundle添加一个密码重复,但我不太确定如何做到这一点。 我的实体中有这两个属性 /** * @var string * * @Assert\NotBlank() * @Assert\Length(max="4096") */ private $plainPassword; /** * @var string * * @ORM\Column(type="string", length=64) */ private $password;

我正在尝试使用easy admin bundle添加一个密码重复,但我不太确定如何做到这一点。 我的实体中有这两个属性

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

/**
 * @var string
 *
 * @ORM\Column(type="string", length=64)
 */
private $password;
如果我在config.yml文件中添加type:repeated,它只会创建两个输入字段,而不是密码类型。我相信形式应该是这样的

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(),
        )
    );
}
我已经查看了easy admin bundle文档,但是我有点不知道如何实现它

谢谢

  • 编辑 好的,我扩展了
    JavierEguiluz\Bundle\EasyAdminBundle\Controller\AdminController的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;
    }
    

但是现在,当我尝试插入/提交表单时,sql错误密码不能为空。

您可以在映射实体的config.yml中设置此密码,例如,我有:

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' } }

注意:控制器定义仅用于更新前使用。

请尝试键入Symfony\Component\Form\Extension\Core\type\PasswordType而不是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'} } }

属性“passwordEnClaro”配置适用于symfony 2.8.*并且为了在symfony 3.2.*中工作
-{property:'plainPassword',type:'password',label:'password',help:'Passwords必须至少有8个字符,{required:false}
由于使用symfony 4 beta 1时表示无法加载类型“密码”,因此应该存在一些问题。bodrak answer提供了symfony 4的解决方案(类型:symfony\Component\Form\Extension\Core\type\PasswordType)