Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [Symfony]Generowanie formularza Ajax/Symfony5
Babulec
post 10.05.2020, 17:49:38
Post #1





Grupa: Zarejestrowani
Postów: 1
Pomógł: 0
Dołączył: 10.05.2020

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


Witam,

próbuję stworzyć formularz w którym po zmianie kategorii generują się dodatkowe pola.

formularz
  1. <?php
  2.  
  3. namespace App\Form;
  4.  
  5. use App\Entity\Advertisement\Advertisement;
  6. ...
  7.  
  8.  
  9. class PostFormType extends AbstractType
  10. {
  11. public function buildForm(FormBuilderInterface $builder, array $options)
  12. {
  13. $builder
  14. ->add('category_select', ButtonType::class, [
  15. 'label'=>'Wybierz kategorię'
  16. ])
  17. ->add('title', TextType::class, [
  18. 'label'=>'Wpisz tytuł'
  19. ])
  20. ->add('category', HiddenType::class)
  21.  
  22. ->add('description', TextareaType::class, [
  23. 'label'=>'Opis'
  24. ])
  25.  
  26. ->add('location', TextType::class, [
  27. 'label'=>'Lokalizacja'
  28. ])
  29. ->add('name', TextType::class, [
  30. 'label'=>'Imię'
  31. ])
  32. ->add('email', EmailType::class, [
  33. 'label'=>'Adres e-mail'
  34. ])
  35. ->add('phone', TextType::class, [
  36. 'label'=>'Telefon'
  37. ])
  38.  
  39.  
  40. ->add('save', SubmitType::class, [
  41. 'attr' => ['class' => 'btn btn-save w-100', 'id' => 'save']
  42. ]);
  43.  
  44. }
  45.  
  46.  
  47. public function configureOptions(OptionsResolver $resolver)
  48. {
  49. $resolver->setDefaults([
  50. 'data_class' => Advertisement::class,
  51. ]);
  52. }
  53. }
  54.  


widok
  1. {{ form_start(advertisementForm, {'attr': {'novalidate': 'novalidate'}}) }}
  2. <div class="container max-width ">
  3. <h1 class="text-center"> Nowe ogłoszenie
  4. </h1>
  5. <h3>Informacje podstawowe</h3>
  6. <div class="form-group">
  7.  
  8. <div class="col_100">
  9. <div class="input-group">
  10. {{ form_label(advertisementForm.title) }}
  11. {{ form_widget(advertisementForm.title) }}
  12. <p class="error" id="error_email"></p>
  13. </div>
  14. {{ form_errors(advertisementForm.title) }}
  15.  
  16. </div>
  17. </div>
  18. <div class="form-group">
  19. <div class="col_100">
  20. {{ form_label(advertisementForm.category_select) }}
  21. </div>
  22. <div class="col_100">
  23. <div class="input-group">
  24. <span id="post_category_name">dsd</span> {{form_widget(advertisementForm.category_select) }}
  25. </div>
  26. </div>
  27. </div>
  28.  
  29. <strong> i tak dalej ....</strong>.
  30.  
  31.  
  32. <div id="content_load_input">
  33.  
  34. <strong>// tutaj chciałbym wczytać nowe pola</strong>
  35.  
  36. </div>
  37.  
  38.  
  39.  
  40. <div class="col-12">
  41. {{ form_widget(advertisementForm.save) }}
  42. </div>
  43. </div>
  44.  
  45.  
  46. {{ form_end(advertisementForm) }}


Na razie robię to tak, że po zmianie kategorii dodaję nowe pola w js

np.
inputs.generateSelect('level', 'Poziom', dataLevel);
inputs.generateSelect('furnishings','Umeblowanie', dataBoolean);

i to oczywiście działa, ale wydaje mi się to strasznie nie po Bożemu...

A więc próbowałem stworzyć drugi formularz i jakoś go zaimportować do pierwszego.

formularz
  1. namespace App\Form;
  2.  
  3. use App\Entity\Advertisement\Flat;
  4. ....
  5.  
  6. class FlatFormType extends AbstractType
  7. {
  8. public function buildForm(FormBuilderInterface $builder, array $options)
  9. {
  10. $builder
  11. ->add('price')
  12. ->add('area');
  13. }
  14.  
  15.  
  16. public function configureOptions(OptionsResolver $resolver)
  17. {
  18. $resolver->setDefaults([
  19. 'data_class' => Flat::class,
  20. ]);
  21. }
  22. }


i ok mogę dodać go tak

->add('flat', FlatFormType::class, [
'mapped' => false
])


ale jak go mogę dodać za pomocą JS po zmianie kategorii?

Przeszukałem już chyba cały internet i albo źle zadaję pytania, albo nie wiem...


Pozdrawiam i z góry dziękuję za pomoc.
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: 28.03.2024 - 12:06