繁体   English   中英

添加下拉到symfony 3表单

[英]Add dropdown to symfony 3 form

我想在symfony版本3的表单中添加一个类别的dropdow字段,我试过解决方案,但每个都有自己的问题

首先得到所有类别并将它们传递给我的视图并显示它们:操作:

 /**
     * 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,
        ));
    }

视图:

{% 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 %}

表单是按照我的预期创建的,但是当我想提交它时,它会显示验证错误,如下所示:

This form should not contain extra fields.

我尝试过的第二个解决方案是从我的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')
    ;
}

通过这种方式,表单也已经很好地创建了但是在提交之后,返回了一个数据库异常并说:

使用params [{},“asdf”,“asdf”]执行'INSERT INTO news(category_id,title,content)VALUES(?,?,?)'时发生异常:

可捕获的致命错误:类AppBundle \\ Entity \\ Category的对象无法转换为字符串

这意味着我的category_id作为对象传递!

我该怎么办?

顺便说一句,我的英语有点弱,请不要在我的帖子上加减,我多次被禁止。

提前致谢。

所有symfony都试图找到Category对象的字符串表示,因此它可以填充选项字段。

你可以通过以下两种方式解决这个问题:

  1. 在Category对象中,您可以创建__toString方法。

     public function __toString() { return $this->name; // or whatever field you want displayed } 

要么

  1. 你可以告诉symfony哪个字段用作字段的标签。 来自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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM