如何在简易管理包中添加重复的类型字段

问题描述 投票:2回答:2

我正在尝试使用简单的管理包添加密码重复,但我不太清楚如何做到这一点。我的实体中有这两个属性

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

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

如果我在config.yml文件中添加一个类型:重复它只创建两个输入字段但不是密码类型。我相信形式应该是这样的。

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(),
        )
    );
}

我已经检查了简单的管理包文档,但我有点失去了如何实现它。 https://github.com/javiereguiluz/EasyAdminBundle/blob/master/Resources/doc/book/7-complex-dynamic-backends.md

谢谢

  • 编辑好的,所以我扩展了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错误密码不能为null。

symfony symfony2-easyadmin
2个回答
1
投票

你可以在映射你的实体的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' } }

注意:控制器定义仅适用于PreUpdate。


1
投票

尝试使用类型Symfony \ Component \ Form \ Extension \ Core \ Type \ PasswordType而不是密码。

        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'} } }
© www.soinside.com 2019 - 2024. All rights reserved.