Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [Symfony] Pobieranie danych przez Ajax do pola Select
grda
post 2.01.2018, 11:52:39
Post #1





Grupa: Zarejestrowani
Postów: 3
Pomógł: 0
Dołączył: 2.01.2018

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


Witam. Mam problem z pobraniem danych przez Ajax do Selecta. Założenie jest takie: Mam dwa pola select. Do pierwszego są pobierane nazwy producentów z bazy przy starcie skryptu. Na podstawie jaka nazwa zostanie wybrana przez użytkownika takimi danymi o modelach ma się zapełnić drugi select.

GetModelType:

  1. class GetModelType extends AbstractType
  2. {
  3. public function buildForm(FormBuilderInterface $builder, array $options)
  4. {
  5. $builder
  6. ->add('brandName', EntityType::class, array(
  7. 'class' => Brand::class,
  8. 'placeholder' => '',
  9. ));
  10. ;
  11.  
  12. $formModifier = function (FormInterface $form, Brand $brand = null) {
  13. $models = null === $brand ? array() : $brand->getModels();
  14.  
  15. $form->add('modelName', EntityType::class, array(
  16. 'class' => Model::class,
  17. 'placeholder' => '',
  18. 'choices' => $models,
  19. ));
  20. };
  21.  
  22. $builder->addEventListener(
  23. FormEvents::PRE_SET_DATA,
  24. function (FormEvent $event) use ($formModifier) {
  25.  
  26. $data = $event->getData();
  27.  
  28. $formModifier($event->getForm(), $data->getModels());
  29. }
  30. );
  31.  
  32. $builder->get('brandName')->addEventListener(
  33. FormEvents::POST_SUBMIT,
  34. function (FormEvent $event) use ($formModifier) {
  35.  
  36. $brand = $event->getForm()->getData();
  37.  
  38. $formModifier($event->getForm()->getParent(), $brand);
  39. }
  40. );
  41. }
  42.  
  43. public function configureOptions(OptionsResolver $resolver)
  44. {
  45. $resolver->setDefaults(array(
  46. 'data_class' => Brand::class,
  47. ));
  48. }
  49.  
  50. // ...
  51. }


Controller:

  1. /**
  2.   * @Route("/aaa", name="create")
  3.   * @param Request $request
  4.   * @return \Symfony\Component\HttpFoundation\Response
  5.   */
  6. public function createAction(Request $request)
  7. {
  8. $meetup = new Brand();
  9. $form = $this->createForm(GetModelType::class, $meetup);
  10. $form->handleRequest($request);
  11. if ($form->isSubmitted() && $form->isValid()) {
  12.  
  13. }
  14.  
  15. return $this->render(
  16. 'default/create.html.twig',
  17. array('form' => $form->createView())
  18. );
  19. }


Próbowałem wykorzystać przykład z dokumentacji,ale pojawia się bład:

Type error: Argument 2 passed to AppBundle\Form\GetModelType::AppBundle\Form\{closure}() must be an instance of AppBundle\Entity\Brand or null, instance of Doctrine\Common\Collections\ArrayCollection given, called in /home/.../src/AppBundle/Form/GetModelType.php on line 49

Mógłby mi ktoś wskazać gdzie popełniłem błąd,bo poruszam się tu po omacku sad.gif
Go to the top of the page
+Quote Post
LowiczakPL
post 7.01.2018, 18:42:56
Post #2





Grupa: Zarejestrowani
Postów: 531
Pomógł: 55
Dołączył: 3.01.2016
Skąd: Łowicz

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


W przypadku dynamicznego generowania formularza na podstawie innego pola musisz zabezpieczyć walidację

w FORMie

  1. $resolver->setDefaults(array(
  2. 'validation_groups' => false,
  3. ));


ewentualnie dla postSubmit

  1. $event->stopPropagation();


w Kontrolerze zabezpieczyć przed wykonaniem kodu dla POSTa AJAX

  1. if ($form->isValid() && !$request->isXmlHttpRequest()) {
  2. // ...
  3. }


--------------------
Szukam zleceń Symfony, Laravel, Back-End, Front-End, PHP, MySQL ...
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: 26.04.2024 - 22:34