Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [Symfony] Podczas tworzenia formularza do edycji już istniejącego elementu formularz nadal oczekuje instancji pliku.
Rymtumtum
post 7.06.2019, 10:50:54
Post #1





Grupa: Zarejestrowani
Postów: 4
Pomógł: 0
Dołączył: 1.11.2016

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


Witam,
podczas tworzenia formularza do edycji już istniejącego elementu formularz napotkałem na problem, że formularz nadal oczekuje instancji pliku.

Formularz:
  1. <?php
  2.  
  3. namespace App\Form;
  4.  
  5.  
  6. use App\Entity\Post;
  7. use Symfony\Component\Form\AbstractType;
  8. use Symfony\Component\Form\Extension\Core\Type\DateType;
  9. use Symfony\Component\Form\Extension\Core\Type\FileType;
  10. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  11. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  12. use Symfony\Component\Form\Extension\Core\Type\TextType;
  13. use Symfony\Component\Form\FormBuilderInterface;
  14. use Symfony\Component\OptionsResolver\OptionsResolver;
  15.  
  16. class PostType extends AbstractType
  17. {
  18. public function buildForm(FormBuilderInterface $builder, array $options)
  19. {
  20. $builder
  21. ->add('title', TextType::class, ['label' => 'Tytuł'])
  22. ->add('subtitle', TextType::class, ['label' => 'Podtytuł'])
  23. ->add('image', FileType::class, ['label' => 'Zdjęcie'])
  24. ->add('description', TextareaType::class, ['label' => 'Opis'])
  25. ->add('contents', TextareaType::class, ['label' => 'treć'])
  26. ->add('date', DateType::class, ['label' => 'Data'])
  27. ->add('author', TextType::class, ['label' => 'Autor'])
  28. ->add('submit', SubmitType::class, ['label' => 'Dodaj'])
  29. ;
  30. }
  31.  
  32. public function configureOptions(OptionsResolver $resolver)
  33. {
  34. $resolver->setDefaults(['data_class' => Post::class]);
  35. }
  36. }


Entity: (skrócone tylko do dotyczącego problemu)
  1. <?php
  2.  
  3. namespace App\Entity;
  4.  
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Symfony\Component\Validator\Constraints as Assert;
  7. use Gedmo\Mapping\Annotation as Gedmo;
  8.  
  9. /**
  10.  * @ORM\Entity(repositoryClass="App\Repository\PostRepository")
  11.  */
  12.  
  13. class Post
  14. {
  15. /**
  16.   * @ORM\Column(type="string")
  17.   *
  18.   * @Assert\NotBlank(message="Please, upload the product brochure as a JPG file.")
  19.   * @Assert\File(mimeTypes={ "image/jpeg" })
  20.   */
  21. private $image;
  22.  
  23. public function getImage()
  24. {
  25. return $this->image;
  26. }
  27.  
  28. public function setImage($image)
  29. {
  30. $this->image = $image;
  31.  
  32. return $this;
  33. }
  34. }


Controller: (skrócone tylko do dotyczącego problemu)
  1. /**
  2.  * @Route("/edit/{slug}", name="edit_post")
  3.  * @param Request $request
  4.  * @param Post $post
  5.  * @return Response
  6.  */
  7. public function editAction(Request $request, Post $post)
  8. {
  9. $post->setImage(
  10. new File($this->getParameter('images_directory').'/'.$post->getImage())
  11. );
  12.  
  13. $editForm = $this->createForm(PostType::class, $post);
  14.  
  15. if ($request->isMethod('post')) {
  16. $editForm->handleRequest($request);
  17.  
  18. $entityManager = $this->getDoctrine()->getManager();
  19. $entityManager->persist($post);
  20. $entityManager->flush();
  21.  
  22. return $this->redirectToRoute('blog_single', ['slug' => $post->getSlug()]);
  23. }
  24.  
  25. return $this->render('actionBlog/edit.html.twig', [
  26. 'editForm' => $editForm->createView(),
  27. ]);
  28. }


Dodam tylko, że jak dam
  1. var_dump($post);
To plik img jest widoczny! Tylko dlaczego nie ładuje go do formularza??
Tak samo w profilerze plik jest widoczny

Jak ktoś już uświadczył podobnego problemu bardzo proszę o pomoc.
Go to the top of the page
+Quote Post
destroyerr
post 7.06.2019, 17:30:08
Post #2





Grupa: Zarejestrowani
Postów: 879
Pomógł: 189
Dołączył: 14.06.2006
Skąd: Bytom

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


W jaki sposób ma załadować do formularza? Co rozumiesz przez ładowanie pliku do formularza?
Go to the top of the page
+Quote Post

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.03.2024 - 11:19