Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [Symfony]Upload wielu plików, problem z CollectionType
PawelC
post 27.02.2018, 23:20:37
Post #1





Grupa: Zarejestrowani
Postów: 1 173
Pomógł: 121
Dołączył: 24.09.2007
Skąd: Toruń

Ostrzeżenie: (0%)
-----


Witam,
Piszę aplikację w symfony która daje możliwość uploadu wielu plików na raz. Niestety mam problem z formularzem. Mam następujący kod
Klasa Entity
  1. <?php
  2.  
  3. namespace AppBundle\Entity;
  4.  
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\ORM\Mapping as ORM;
  7.  
  8. /**
  9.  * @ORM\Entity
  10.  */
  11. class DiaryContent
  12. {
  13. /**
  14.   * @ORM\Column(type="integer")
  15.   * @ORM\Id
  16.   * @ORM\GeneratedValue(strategy="AUTO")
  17.   */
  18. protected $id;
  19.  
  20. /**
  21.   * @ORM\Column(type="integer")
  22.   */
  23. protected $author_id;
  24.  
  25. /**
  26.   * @ORM\Column(type="text")
  27.   */
  28. protected $title;
  29.  
  30. /**
  31.   * @ORM\Column(type="text")
  32.   */
  33. protected $content;
  34.  
  35. /**
  36.   * @ORM\Column(type="datetime")
  37.   */
  38. protected $data;
  39.  
  40.  
  41. /**
  42.   * @ORM\Column(type="text")
  43.   */
  44. protected $images;
  45.  
  46. public function __construct()
  47. {
  48. $this->images = new ArrayCollection();
  49. }
  50.  
  51. /**
  52.   * @return mixed
  53.   */
  54. public function getId()
  55. {
  56. return $this->id;
  57. }
  58.  
  59. /**
  60.   * @param mixed $id
  61.   */
  62. public function setId($id)
  63. {
  64. $this->id = $id;
  65. }
  66.  
  67. /**
  68.   * @return mixed
  69.   */
  70. public function getAuthorId()
  71. {
  72. return $this->author_id;
  73. }
  74.  
  75. /**
  76.   * @param mixed $author_id
  77.   */
  78. public function setAuthorId($author_id)
  79. {
  80. $this->author_id = $author_id;
  81. }
  82.  
  83. /**
  84.   * @return mixed
  85.   */
  86. public function getTitle()
  87. {
  88. return $this->title;
  89. }
  90.  
  91. /**
  92.   * @param mixed $title
  93.   */
  94. public function setTitle($title)
  95. {
  96. $this->title = $title;
  97. }
  98.  
  99. /**
  100.   * @return mixed
  101.   */
  102. public function getContent()
  103. {
  104. return $this->content;
  105. }
  106.  
  107. /**
  108.   * @param mixed $content
  109.   */
  110. public function setContent($content)
  111. {
  112. $this->content = $content;
  113. }
  114.  
  115. /**
  116.   * @return mixed
  117.   */
  118. public function getData()
  119. {
  120. return $this->data;
  121. }
  122.  
  123. /**
  124.   * @param mixed $data
  125.   */
  126. public function setData($data)
  127. {
  128. $this->data = $data;
  129. }
  130.  
  131. /**
  132.   * @return mixed
  133.   */
  134. public function getImages()
  135. {
  136. return $this->images;
  137. }
  138.  
  139. /**
  140.   * @param mixed $images
  141.   */
  142. public function setImages($images)
  143. {
  144. $this->images = $images;
  145. }
  146. }


Kod formularza
  1. <?php
  2.  
  3. namespace AppBundle\Form;
  4.  
  5. use Symfony\Component\Form\AbstractType;
  6. use Symfony\Component\Form\Extension\Core\Type\CollectionType;
  7. use Symfony\Component\Form\Extension\Core\Type\FileType;
  8. use Symfony\Component\Form\FormBuilderInterface;
  9. use Symfony\Component\OptionsResolver\OptionsResolver;
  10.  
  11. class DiaryContentType extends AbstractType
  12. {
  13. /**
  14.   * {@inheritdoc}
  15.   */
  16. public function buildForm(FormBuilderInterface $builder, array $options)
  17. {
  18. $builder->add('author_id')->add('title')->add('content')->add('data')->add('images', CollectionType::class, array(
  19. 'entry_type' => FileType::class,
  20.  
  21. ));
  22. }
  23.  
  24. /**
  25.   * {@inheritdoc}
  26.   */
  27. public function configureOptions(OptionsResolver $resolver)
  28. {
  29. $resolver->setDefaults(array(
  30. 'data_class' => 'AppBundle\Entity\DiaryContent'
  31. ));
  32. }
  33.  
  34. /**
  35.   * {@inheritdoc}
  36.   */
  37. public function getBlockPrefix()
  38. {
  39. return 'appbundle_diarycontent';
  40. }
  41.  
  42.  
  43. }

oraz jego wywołanie w kontrolerze
  1. $diaryContent = new DiaryContent();
  2.  
  3. $form = $this->createFormBuilder($diaryContent)
  4. ->add("title", TextType::class)
  5. ->add('content', TextareaType::class)
  6. ->add('images', CollectionType::class, array(
  7. 'entry_type' => FileType::class,
  8. 'required' => false,
  9. 'mapped' => false,
  10. 'allow_add' => true,
  11. ))->getForm();
  12.  
  13. return $this->render('@App/Diary/new.html.twig', array(
  14. 'form' => $form->createView(),
  15. ));


Problem jest taki, że wyświetlają mi się wszystkie pola formularza, poza tym z CollectionType, jak zmienię na FileType to pokazuje tylko jeden plik. Ma ktoś jakieś sensowne pomysły? Ogólnie chciałbym zrobić formularz do uploadu zdjęć bez limitu, ale mi nie idzie.
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 19.07.2025 - 10:30