控制器调用具有EntityType的表单以选择要编辑的实体。表单的模板不会返回具有实体ID的控制器。我以为我知道如何做到这一点,但我尝试了多种变化而没有成功。现状:
控制器:
/**
* @Route("/edit/{id}", name="artist_edit")
*/
public function editArtistAction(Request $request, Defaults $defaults, $id = null)
{
if (null !== $id) {
$em = $this->getDoctrine()->getManager();
$artist = $em->getRepository('AppBundle:Artist')->find($id);
} else {
$form = $this->createForm(SelectArtistType::class, null, ['target' => 'edit']);
return $this->render('Artist/selectArtist.html.twig',
[
'form' => $form->createView(),
]);
}
...
}
形式:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$target = $options['target'];
$builder
->add('artist', EntityType::class,
[
'class' => 'AppBundle:Artist',
'label' => 'Select artist for ' . $target,
'choice_label' => function($artist, $key, $index) {
return $artist->getLastName() . ', ' . $artist->getFirstName();
},
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('a')
->orderBy('a.firstName', 'ASC')
->orderBy('a.lastName', 'ASC');
},
])
->add('save', SubmitType::class,
array(
'label' => 'Select',
'label_format' => ['class' => 'text-bold']
));
}
模板:
{% extends "base.html.twig" %}
{% block body %}
<div class="row">
<div class="col-sm-4"></div>
<div class="col-sm-4">
{{ form_start(form) }}
{{ form_row(form.artist) }}
{{ form_rest(form) }}
{{ form_row(form.save) }}
{{ form_end(form) }}
</div>
</div>
{% endblock body %}
答案 0 :(得分:0)
您不在任何地方读取来自POST的数据。在您的控制器中,您应该在收到帖子时检查请求。把这样的东西放在控制器的开头,这样你就可以看到你在POST中收到的内容了:
if ($request->isMethod('POST')) {
var_dump($request->request)
}
然后按照您喜欢的方式处理它。