简体   繁体   中英

Nested Form / Mapping field to entity

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM