Symfony2将表单数据发送到数据库。什么都没发生

时间:2015-05-18 09:31:16

标签: php symfony

这看起来像一个简单的任务。我这样做是通过使用文档,但我只是没有创建一个新用户。我没有得到任何错误,但我也没有得到用户。我在一个单独的类中创建我的表单,如下所示:

class RegisterFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options) {

                $builder                

                ->add('firstname', 'text', array(
                'label' => 'First Name * ',
                'attr' => array('placeholder' => 'First Name')))
                ->add('email', 'email', array(
                'label' => 'Email * ',
                'attr' => array('placeholder' => 'Email')))
                ->add('password', 'password', array(
                'label' => 'Password * ',
                'attr' => array('placeholder' => 'Password')))

                ->add('save', 'submit', array('label' => 'Register'));



    }

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'Mp\ShopBundle\Entity\Users',
    ));
}

public function getName()
    {
        return 'register_form_users';
    }
}

在我的第二个控制器中,我获取数据库,创建新用户并将信息添加到该用户?至少我认为我......这里有什么问题?

public function registerAction(Request $request)
    {   
            $em = $this->getDoctrine()->getManager();
            $products = $em->getRepository('MpShopBundle:Product')->findAll();

            $user = new Users();

            $form = $this->createForm(new RegisterFormType(), $user);


            if ($form->isValid()) {

                $firstname = $form->get('firstname')->getData();
                $user->setFirstname($firstname);
                $email = $form->get('email')->getData();
                $user->setEmail($email);
                $password = $form->get('password')->getData();
                $user->setPassword($password);

                $em->persist($user);
                $em->flush();

            }

                return $this->render('MpShopBundle:Frontend:registration.html.twig',  array(
               'products'=>$products,
               'form'=>$form->createView(),    
               ));  
    }

2 个答案:

答案 0 :(得分:2)

$form->handleRequest($request)之前添加$form->isValid(),它应该有效。因为您正在创建表单但不会从请求中处理数据。如果需要,您可以查看source

答案 1 :(得分:0)

public function registerAction(Request $request)
{   
        $em = $this->getDoctrine()->getManager();
        $products = $em->getRepository('MpShopBundle:Product')->findAll();

        $user = new Users();

        $form = $this->createForm(new RegisterFormType(), $user);

        /** add this code */
        $form->handleRequest($request);
        /** add this code */


        if ($form->isValid()) {

            $firstname = $form->get('firstname')->getData();
            $user->setFirstname($firstname);
            $email = $form->get('email')->getData();
            $user->setEmail($email);
            $password = $form->get('password')->getData();
            $user->setPassword($password);

            $em->persist($user);
            $em->flush();

        }

            return $this->render('MpShopBundle:Frontend:registration.html.twig',  array(
           'products'=>$products,
           'form'=>$form->createView(),    
           ));  
}