使用2个不同类别的注册表类型

时间:2019-02-24 03:56:18

标签: php symfony

我在symfony中为用户创建了一个注册表,这些用户与另一个表(城市)的另一个字段相关,我希望登录的用户可以选择自己的城市,但是我无法获取要添加的注册表我是用户通过下拉列表选择的城市。它真的很麻烦。

这是注册表格的代码:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('email')
        ->add('Password', PasswordType::class, [
            // instead of being set onto the object directly,
            // this is read and encoded in the controller
            'mapped' => false,
            'constraints' => [
                new NotBlank([
                    'message' => 'Please enter a password',
                ]),
                new Length([
                    'min' => 6,
                    'minMessage' => 'Your password should be at least {{ limit }} characters',
                    // max length allowed by Symfony for security reasons
                    'max' => 4096,
                ]),
            ],
        ])
        ->add('nombre')
        ->add('apellidos')
        //Falta por añadir el mensaje y la ciudad
    ;
}

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
        'data_class' => User::class,
    ]);
}

这是注册控制器的代码

    /**
 * @Route("/register", name="app_register")
 */
public function register(Request $request, UserPasswordEncoderInterface $passwordEncoder): Response
{
    $user = new User();
    $form = $this->createForm(RegistrationFormType::class, $user);
    $form->handleRequest($request);
    if ($form->isSubmitted() && $form->isValid()) {
        // encode the plain password
        $user->setPassword(
            $passwordEncoder->encodePassword(
                $user,
                $form->get('Password')->getData()
            )
        );
        //El usuario empezara con 0 minutos de saldo cuando se registre
        $user->setTiempo(0);

        $entityManager = $this->getDoctrine()->getManager();
        $entityManager->persist($user);
        $entityManager->flush();

        // do anything else you need here, like send an email

        return $this->redirectToRoute('index');
    }

    return $this->render('registration/register.html.twig', [
        'registrationForm' => $form->createView(),
    ]);
}

1 个答案:

答案 0 :(得分:2)

那是因为您没有将城市添加到表单中。您必须添加它。为此,您必须选择...

  1. 使用实体本身(EntityType)
  2. 为城市创建自定义FormType

据您所知,我希望使用EntityType。像这样...并详细了解EntityType / CustomType @documentation https://symfony.com/doc/current/reference/forms/types/entity.html

$builder
        ->add('email')
        ->add('cities', EntityType::class, [
            'class' => Cities::class,
            'choice_label' => 'name',
            'choice_value' => 'id'
        ])
        ->add('Password', PasswordType::class, [
            // instead of being set onto the object directly,
            // this is read and encoded in the controller
            'mapped' => false,
            'constraints' => [
                new NotBlank([
                    'message' => 'Please enter a password',
                ]),
                new Length([
                    'min' => 6,
                    'minMessage' => 'Your password should be at least {{ limit }} characters',
                    // max length allowed by Symfony for security reasons
                    'max' => 4096,
                ]),
            ],
        ])
        ->add('nombre')
        ->add('apellidos')
        //Falta por añadir el mensaje y la ciudad
    ;
相关问题