رتبه موضوع:
  • 0 رای - 0 میانگین
  • 1
  • 2
  • 3
  • 4
  • 5
افزودن dropdown به فرم توی symphony 3
#1
سلام دوستان
می خوام یه dropdown مربوط به دسته بندی اضافه کنم هر کاری می کنم خطا می ده:
وقتی مستقیم می برم توی view موقع submit خطا می ده که :

نقل قول:
This form should not contain extra fields.


اینم کد این قسمت:
action:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
   /**
    * 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();
 
           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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{% 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 می فرسته و خطا دیتابیس می ده:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 /**
    * @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) {
                    return $category->getTitle();
                }
            ])
           ->add('title')
           ->add('content')
       ;
   }
no pain, no gain
پاسخ
تشکر شده توسط:




کاربران در حال بازدید این موضوع: