当我在一个类中显示我的表单时,Symfony2没有任何反应 [英] Symfony2 nothing happens when I display my form inside a class
本文介绍了当我在一个类中显示我的表单时,Symfony2没有任何反应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<?php $ b 所以我正在关注文档,并在自己的类中创建一个表单。 $ b命名空间Mp\\ShopBundle\Form\Type;
使用Symfony \Component\Form\AbstractType;
使用Symfony \Component\Form\FormBuilder;
使用Symfony \Component\Form\FormBuilderInterface;
使用Symfony \Component\Form\FormView;
使用Symfony \Component\Form\FormInterface;
使用Symfony \Component\OptionsResolver\Options;
使用Symfony \ Component\OptionsResolver\OptionsResolverInterface;
使用Symfony \ Component \PropertyAccess\PropertyAccess;
class RegisterFormType extends AbstractType
{
public function registerForm(FormBuilderInterface $ builder,array $ options){
$ builder
> add('title','choice',array(
'choices'=> array(' - '=>' - ','mr'=>'Mr.',' ''','''',''''),
'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'=> ;'地址*',
'attr'=>数组('placeholder'=>'地址')))
- > add('Country','country',array(
'label'=>'Country *',
'attr'=> array('placehol ('''&';'''''))
- > 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'=> ;'邮政编码*',
'attr'=> array('placeholder'=>'邮政编码')))
- > add('AdditionalInformation',' ('placeholder'=&''Additional Information')))
- 数组(
'label'=>'Additional Information',
'attr'=& > add('HomePhone','number',array(
'label'=>'家庭电话',
'attr'=> array('placeholder'=>'家庭电话')))
- > add('MobilePhone','number' ,数组(
'label'=>'Mobile phone',
'attr'=> array('placeholder'=>'Mobile Phone')))
- > add('save','submit',array('label'=> '寄存器'));
$ b public function getName()
{
return'register_form_users';
}
}
它看起来很简单。现在在我的控制器中,我想显示它:
使用Mp \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
public function registerAction()
{
$ em = $ this-> getDoctrine() - > getManager();
$ products = $ em-> getRepository('MpShopBundle:Product') - > findAll();
$ form = $ this-> createForm(new RegisterFormType());
$ b $ return $ this-> render('MpShopBundle:Frontend:registration.html.twig',array(
'products'=> $ products,
'form' => $ form-> createView(),
));
$ / code>
我的小枝:
< h3>您的个人资讯< / h3>
{{dump(form)}}
{%form_theme form _self%}
{{form(form)}}
事情就是没有得到我的表格。
当我做 {{dump(表单)}} $ c $时,页面和模板加载正常,但不是我的表单。 c>我得到了一些东西:
FormView {#2110▼
+ vars:array:33 [▶]
+ parent:null
+ children:array:1 [▶]
-rendered:true
}
正如您所看到的,我正在获取表单?但它不显示?...为什么是这样?
解决方案
您必须更改您的方法
public function registerForm(FormBuilderInterface $ builder,array $ options){
to
public function buildForm(FormBuilderInterface $ builder,array $ options)
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?
解决方案
You must change your method
public function registerForm(FormBuilderInterface $builder, array $options) {
to
public function buildForm(FormBuilderInterface $builder, array $options)
这篇关于当我在一个类中显示我的表单时,Symfony2没有任何反应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文