douwen1937 2017-04-06 08:57
浏览 45
已采纳

嵌套表单/映射字段到实体

I'm looking for best (or just working) way to solve following problem. I have like standard UserType form

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add(
            'username',
            Type\TextType::class
        )
        ->add(
            'email',
            Type\EmailType::class
        )
        ->add(
            'plainPassword',
            Security\UserRepeatedPasswordType::class
        )
        ->add(
            'roles',
            Type\ChoiceType::class,
            [
                'multiple' => true,
                'expanded' => true,
                'choices' => $this->getRoleChoices()
            ]
        );
}

What is nonstandard is that UserRepeatedPasswordType, it looks like this

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add(
            'password',
            Type\RepeatedType::class,
            [
                'type' => Type\PasswordType::class,
                'required' => true,
                'first_options'  => [
                    'label' => 'Password'
                ],
                'second_options' => [
                    'label' => 'Repeat Password'
                ],
            ]
        );
}

And I created it because those two fields are also used in passwordReset form and userSettings form. And now I have two problems:

1.) When I use it this way, value from UserRepeatedPasswordType is not correctly mapped for my User Entity - there is an error that string is expected (duh ;) but it got array. I tried using View and Model transformer but no proper results (but I don't have much experience with those, so that maybe the case). I also tried to experiment with getParent(), and pass there UserType but it goes to some endless loop and I got 500. If I just copy paste field from UserRepeatedPasswordType to UserType it works correctly.

2.) If this is solved (or even by copy paste, if can't be done other way), there is another related (I believe) problem:

I have this ChangePasswordType form, which is used to reset your password.

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add(
            'confirmationToken',
            Type\HiddenType::class,
            [
                'required' => true,
                'constraints' => [
                    new NotBlank(),
                ]
            ]
        )
        ->add(
            'plainPassword',
            Type\RepeatedType::class,
            [
                'type' => Type\PasswordType::class,
                'required' => true,
                'first_options'  => [
                    'label' => 'Password'
                ],
                'second_options' => [
                    'label' => 'Repeat Password'
                ],
            ]
        )
        ->add(
            'changePassword',
            Type\SubmitType::class
        );
}

And it works fine as it is but I want to do two things with it - first, solving my first problem and use UserRepeatedPasswordType in it, second - I have some Assert\Length done in User Entity on $plainPassword and it workes correctly when I submit new user via UserType form. But I want that validation somewhat mapped to ChangePasswordType or ideally to UserRepeatedPasswordType - just to have all rules in one place. Can this even be done? Thanks for any solutions / hints / advices.

  • 写回答

1条回答 默认 最新

  • doob0526 2017-04-10 07:26
    关注

    Ok, dunno if anyone is interested but that is how I completed this. If anyone have better answer, just give me a sign (mostly to the first one) ;)

    1.) As i thought, solved by ViewTransformer but in parent form (In UserType not in UserRepeatedPasswordType

    $builder->get('plainPassword')
        ->addViewTransformer(new CallbackTransformer(
            function ($singleAsArray) {
                return $singleAsArray;
            },
            function ($arrayAsSingle) {
                return $arrayAsSingle['password'] ?? '';
            }
        ));
    

    2.) That was actually quite easy. All you have to do is to map that form to UserEntity that same way as UserType and made custom validation groups just to have everything nice and under control :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因