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 :)

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

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧