所以我正在关注文档,我正在自己的类中创建一个表单:
<?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';
}
}
它看起来像一个简单的形式。现在在我的控制器中我想要显示它:
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(),
));
}
我的树枝:
<h3>Your personal information</h3>
{{ dump(form) }}
{% form_theme form _self %}
{{ form(form) }}
事情是我没有得到我的形式。页面和模板加载正常,但不是我的表单。
当我{{ dump(form) }}
时,我得到了一些东西:
FormView {#2110 ▼
+vars: array:33 [▶]
+parent: null
+children: array:1 [▶]
-rendered: true
}
你可以看到我收到表格?但它没有展示?......为什么会这样?
答案 0 :(得分:0)
您必须更改方法
public function registerForm(FormBuilderInterface $builder, array $options) {
到
public function buildForm(FormBuilderInterface $builder, array $options)