大家好,我有问题。我需要从由实体类填充的“选择”字段中获取数据。
这是我的实体:这是包含月份,第一天和最后一天的字典。
实体
namespace accountant\ReportBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class Calendar
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="month", type="string", length=255)
*/
private $month;
}
我将此表的夹具加载到db并创建formBuilder:
FormType
class ReportFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('month','entity', array(
'class' => 'ReportBundle:Calendar',
'property' => 'month',
'expanded' => false,
'multiple' => false
));
}
}
现在,我想获取选定月份到的数据,但是$data
是null
:
控制器
/**
* @Route("/report", name="report")
* @Template()
*/
public function indexAction(Request $request)
{
$form = $this->createForm(new ReportFormType());
$form->handleRequest($request);
$data = $form->getData();
var_dump($data); // $data is null!
return array('form' => $form->createView());
}
TWIG
<form method="get" action="{{ path('report') }}">
<div class="form-group">
{{ form_row(form.month, {'label': 'Select Month:', 'attr': {'class': 'form-control'}}) }}
</div>
<div class="form-group">
<input type="submit" value="Show Report" class="btn btn-warning btn-block"/>
</div>
</form>
更新1
我添加了“ data_class”,但我仍然得到NULL。
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('month','entity', array(
'class' => 'ReportBundle:Calendar',
'choice_label' => 'month',
'expanded' => false,
'multiple' => false,
));
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => Calendar::class
));
}
这是我的控制器。
public function indexAction(Request $request)
{
$form = $this->createForm(new ReportFormType());
$form->handleRequest($request);
$data = $form->get('month')->getData();
// i check data
var_dump($data);
return array(
'form' => $form->createView());
}
更新2
我解决了我的问题,为此我在模板中更改了表单:
<form method="post" action="{{ path('report') }}" novalidate="novalidate">
{{ form_errors(form) }}
<div class="form-group">
{{ form_row(form.month, {'label': 'Select month', 'attr': {'class': 'form-control'}}) }}
{{ form_rest(form) }}
</div>
<div class="form-group">
<input type="submit" value="Show report" class="btn btn-warning btn-block"/>
</div>
我将方法更改为post
并添加form_rest
答案 0 :(得分:1)
像这样更改您的表单:
use accountant\ReportBundle\Entity\Calendar;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class ReportFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('month','entity', array(
'class' => 'ReportBundle:Calendar',
'property' => 'month',
'expanded' => false,
'multiple' => false
));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class'=> Calendar::class,
));
}
}
请记住,CAPS中使用命名空间部分的首字母(Accountant
而不是accountant
)。
根据OP的评论,您可能会在控制器中获得month
数据:
Symfony 2
public function indexAction(Request $request)
{
// ...
$data = $form->get('month')->getData();
// ...
}
Symfony 3和4
public function indexAction(Request $request)
{
// ...
$data = $form['month']->getData();
// ...
}