Php 使用Symfony2获取表单中的数据失败

Php 使用Symfony2获取表单中的数据失败,php,forms,symfony,post,null,Php,Forms,Symfony,Post,Null,我的控制器是: $form = $this->createForm(new Links()); $peticion = $this->getRequest(); if ($peticion->getMethod() == 'POST') { $form->bind($peticion); if ($form->isValid()) { $data = $form->getDat

我的控制器是:

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

$peticion = $this->getRequest();

if ($peticion->getMethod() == 'POST') {
            $form->bind($peticion);
            if ($form->isValid()) {
                $data = $form->getData();  
            }   
        }
我有一个实体“Links”和一个名为“Links”的表单类,但是在controller中,在$data中,表单的所有字段都是空的

----------形式类

public function buildForm(FormBuilderInterface $builder, array $options)
    {      
        $builder
            ->add('licenses', integer, array('label' => 'Number Licenses', 'required' => false))
            ->add('Years', 'choice', array('choices'=> array('y1' => '1 year', '3y' => '3 years'),'label' => 'Years license', 'required' => false))
            ->add('Type', 'choice', array('choices'=> array('TypeA' => 'Type A', 'TypeB' => 'TypeB'),'label' => 'License Type', 'required' => false));
    }

public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Me\WebBundle\Entity\Links'
        ));
    }
----------添加视图

<form action="<?php echo $view['router']->generate('generate_links') ?>" method="post" <?php echo $view['form']->enctype($form) ?> >
    <fieldset>
        <legend>Generate Links</legend>
        <?php echo $view['form']->widget($form) ?>
    </fieldset>
    <input class="profile_button" value="Generate link" type="submit" />

</form>
>
生成链接

谢谢。

鉴于您的表单类是LinksType,实体是Links:

$entity = new Links();
$form = $this->createForm(new LinksType(), $entity);

$peticion = $this->getRequest();

if ($peticion->getMethod() == 'POST') {
            $form->bind($peticion);
            if ($form->isValid()) {
                //At this point $entity is populated with data.
                //You wouldn't need form->getData() to access data:
                //Next two lines would have the same output:
                $this->get('logger')->debug($entity->getLicences());
                $this->get('logger')->debug($form->get('licences')->getData());
                $data = $form->getData();
    }   
}

你能出示你的表格吗?是的,只是在原来的帖子里加了。Thanksin setDefaultOptions()您设置了
数据类
,对吗?是的,我在原始帖子中添加了这个函数,谢谢。哪个版本的Symfony?另外,您可以将html与表单一起发布。您可能希望仅对未映射的表单字段使用getData()('mapped'=>false)