خرید بک لینک
سلام دوستان
می خوام یه dropdown مربوط به دسته بندی اضافه کنم هر کاری می کنم خطا می ده:
وقتی مستقیم می برم توی view موقع submit خطا می ده که :

کد:

This form should not contain extra fields.

اینم کد این قسمت:
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('AppBundleFormNewsType', $news);
       $form->handleRequest($request);

       if ($form->isSubmitted() && $form->isValid()) {
           $em = $this->getDoctrine()->getManager();
           $em->persist($news);
           $em->flush();

           retu $this->redirectToRoute('news_show', array('id' => $news->getId()));
       }

       $em = $this->getDoctrine()->getManager();
       $categories = $em->getRepository('AppBundle:Category')->findAll();

       retu $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 %}

به جای این اومدم از یه روش دیگه استفاده کنم، اومدم buildForm رو تغییر دادم ، وقی بعد از submit می بینم که مقدار category_id رو object می فرسته و خطا دیتابیس می ده:

  /**
     * @param FormBuilderInterface $builder
    * @param array $options
    */
   public function buildForm(FormBuilderInterface $builder, array $options)
   {

       $builder
           ->add('categoryId', EntityType::class, [
                'class' => 'AppBundle:Category',
                'choice_label' => function ($category) {
                    retu $category->getTitle();
                }
            ])
           ->add('title')
           ->add('content')
       ;
   }

برچسب: نویسنده: خنجی تاريخ: چهارشنبه 15 ارديبهشت 1395 ساعت: 19:00

صفحه بندی