[英]Add dropdown to symfony 3 form
I want to add a dropdow field for category in my form in symfony version 3, I have tried to solutions but each one have their own problem 我想在symfony版本3的表单中添加一个类别的dropdow字段,我试过解决方案,但每个都有自己的问题
First got all categories and pass them to my view and show them: Action: 首先得到所有类别并将它们传递给我的视图并显示它们:操作:
/**
* Creates a new News entity.
*
* @Route("/new", name="news_new")
* @Method({"GET", "POST"})
*/
public function newAction(Request $request)
{
$news = new News();
$form = $this->createForm('AppBundle\Form\NewsType', $news);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($news);
$em->flush();
return $this->redirectToRoute('news_show', array('id' => $news->getId()));
}
$em = $this->getDoctrine()->getManager();
$categories = $em->getRepository('AppBundle:Category')->findAll();
return $this->render('news/new.html.twig', array(
'news' => $news,
'form' => $form->createView(),
'categories' => $categories,
));
}
View: 视图:
{% extends 'base.html.twig' %}
{% block body %}
<h1>News creation</h1>
{{ form_start(form) }}
<label for="news_content" class="required">Category</label>
<select name="news[categoryId]">
{% for category in categories %}
<option value="{{ category.id }}">{{ category.title }}</option>
{% endfor %}
</select>
{{ form_widget(form) }}
<input class="btn btn-sm btn-success" type="submit" value="Create" />
{{ form_end(form) }}
<ul>
<li>
<a class="label label-sm label-info" href="{{ path('news_index') }}">Back to the list</a>
</li>
</ul>
{% endblock %}
The form is created as I expected but when i want to submit it, it show an validation error as bellow: 表单是按照我的预期创建的,但是当我想提交它时,它会显示验证错误,如下所示:
This form should not contain extra fields.
second solution that I have tried is to generate the dropdown from my Type, so in NewsType I changed the buildForm function as bellow: 我尝试过的第二个解决方案是从我的Type生成下拉列表,所以在NewsType中我将buildForm函数改为:
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('categoryId', EntityType::class, [
'class' => 'AppBundle:Category',
'choice_label' => 'title',
])
->add('title')
->add('content')
;
}
It this way, the form also have been created nicely but after submit, an database exception returned and said: 通过这种方式,表单也已经很好地创建了但是在提交之后,返回了一个数据库异常并说:
An exception occurred while executing 'INSERT INTO news (category_id, title, content) VALUES (?, ?, ?)' with params [{}, "asdf", "asdf"]: 使用params [{},“asdf”,“asdf”]执行'INSERT INTO news(category_id,title,content)VALUES(?,?,?)'时发生异常:
Catchable Fatal Error: Object of class AppBundle\\Entity\\Category could not be converted to string 可捕获的致命错误:类AppBundle \\ Entity \\ Category的对象无法转换为字符串
It mean that my category_id passed as an object ! 这意味着我的category_id作为对象传递!
What should I do? 我该怎么办?
BTW, my english is a little weak, please do not put and minus on my post, I had been ban multiple times. 顺便说一句,我的英语有点弱,请不要在我的帖子上加减,我多次被禁止。
Thanks in advance. 提前致谢。
all symfony is trying to do is to find a string representation of the Category object, so it can populate the option fields. 所有symfony都试图找到Category对象的字符串表示,因此它可以填充选项字段。
you can solve this in a couple of ways: 你可以通过以下两种方式解决这个问题:
In the Category object, you can make a __toString
method. 在Category对象中,您可以创建__toString
方法。
public function __toString() { return $this->name; // or whatever field you want displayed }
or 要么
you can tell symfony which field to use as the label for the field. 你可以告诉symfony哪个字段用作字段的标签。 From docs 来自docs
$builder->add('attending', ChoiceType::class, array( 'choices' => array( new Status(Status::YES), new Status(Status::NO), new Status(Status::MAYBE), ), 'choice_label' => 'displayName', // <-- where this is getDisplayName() on the object. ));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.