Symfony2 nothing happens when I display my form in

2019-08-15 10:17发布

So I am following the documentation and I am making a form inside its own class:

<?php
namespace Mp\ShopBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\PropertyAccess\PropertyAccess;


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

                $builder                
                >add('title', 'choice', array(
                'choices' => array('-' => '-', 'mr' => 'Mr.', 'mrs' => 'Mrs.', 'mss' => 'Miss.'),
                'label' => 'Title * ',
                'attr' => array('class' => 'span1')))
                ->add('firstName', 'text', array(
                'label' => 'First Name * ',
                'attr' => array('placeholder' => 'First Name')))
                ->add('lastName', 'text', array(
                'label' => 'Last Name * ',
                'attr' => array('placeholder' => 'Last Name')))
                ->add('Email', 'email', array(
                'label' => 'Email * ',
                'attr' => array('placeholder' => 'Email')))
                ->add('Password', 'password', array(
                'label' => 'Password * ',
                'attr' => array('placeholder' => 'Password')))
                ->add('DateOfBirth', 'date', array(
                'label' => 'Date Of Birth * ',
                'widget' => 'choice'))
                ->add('Company', 'text', array(
                'label' => 'Company ',
                'attr' => array('placeholder' => 'Company')))
                ->add('Adress', 'text', array(
                'label' => 'Adress * ',
                'attr' => array('placeholder' => 'Adress')))
                ->add('Country', 'country', array(
                'label' => 'Country * ',
                'attr' => array('placeholder' => 'Country')))
                ->add('State', 'text', array(
                'label' => 'State * ',
                'attr' => array('placeholder' => 'State')))
                ->add('City', 'text', array(
                'label' => 'City * ',
                'attr' => array('placeholder' => 'City')))
                ->add('ZipPostalCode', 'text', array(
                'label' => 'Zip / Postal Code *',
                'attr' => array('placeholder' => 'Zip / Postal Code')))
                ->add('AdditionalInformation', 'textarea', array(
                'label' => 'Additional Information ',
                'attr' => array('placeholder' => 'Additional Information')))
                ->add('HomePhone', 'number', array(
                'label' => 'Home phone ',
                'attr' => array('placeholder' => 'Home Phone')))
                ->add('MobilePhone', 'number', array(
                'label' => 'Mobile phone ',
                'attr' => array('placeholder' => 'Mobile Phone')))
                ->add('save', 'submit', array('label' => 'Register'));


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

It looks like a simple form. Now in my controller I want to show it:

use Mp\ShopBundle\Form\Type\RegisterFormType;

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

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

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

My twig:

<h3>Your personal information</h3>
{{ dump(form) }}
{% form_theme form _self %}

{{ form(form) }}

The thing is im not getting my form. The page and the template loads fine, but not my form.

When I do {{ dump(form) }} I get something:

FormView {#2110 ▼
  +vars: array:33 [▶]
  +parent: null
  +children: array:1 [▶]
  -rendered: true
}

As you can see I am getting the form? But it is not displaying?... Why is that?

1条回答
太酷不给撩
2楼-- · 2019-08-15 10:41

You must change your method

public function registerForm(FormBuilderInterface $builder, array $options) {

to

public function buildForm(FormBuilderInterface $builder, array $options)
查看更多
登录 后发表回答