Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [Symfony][Symfony2][SF2] Button w formularzu, typu collection
Forum PHP.pl > Forum > PHP > Frameworki
pabito
Cześć.

Mam taki problem.
Mam poniższą klasę formularza: DefinitionType. W tym formularzu jest przycisk submit
  1. <?php
  2.  
  3. namespace Lingogo\AdminBundle\Form\Type;
  4.  
  5. use Symfony\Component\Form\AbstractType;
  6. use Symfony\Component\Form\FormBuilderInterface;
  7. use Symfony\Component\OptionsResolver\OptionsResolverInterface;
  8.  
  9. class DefinitionType extends AbstractType
  10. {
  11. public function buildForm(FormBuilderInterface $builder, array $options)
  12. {
  13. $builder->add('partOfSpeech', 'entity', array(
  14. 'class' => 'LingogoAdminBundle:PartOfSpeech',
  15. 'property' => 'name',
  16. 'empty_value' => 'Please select part of speech'
  17. ));
  18.  
  19. $builder->add('definition', 'textarea');
  20. $builder->add('exampleUsage', 'textarea');
  21. $builder->add('add', 'submit');
  22. }
  23. public function setDefaultOptions(OptionsResolverInterface $resolver){
  24. $resolver->setDefaults(array(
  25. 'data_class' => 'Lingogo\AdminBundle\Entity\Definition'
  26. ));
  27. }
  28. /**
  29.   * Returns the name of this type.
  30.   *
  31.   * @return string The name of this type
  32.   */
  33. public function getName()
  34. {
  35. return 'definition';
  36. }
  37. }


Następnie mam inną klasę formularza WordType, która ma pole 'definitions' typu collection. Jak widać pole te jest budowane przez DefinitionType
WordType także posiada przycisk submit


  1. <?php
  2. //src/Lingogo/AdminBundle/Form/WordType.php
  3.  
  4. namespace Lingogo\AdminBundle\Form\Type;
  5.  
  6. use Symfony\Component\Form\AbstractType;
  7. use Symfony\Component\Form\FormBuilderInterface;
  8. use Symfony\Component\OptionsResolver\OptionsResolverInterface;
  9.  
  10. class WordType extends AbstractType
  11. {
  12. public function buildForm(FormBuilderInterface $builder, array $options)
  13. {
  14. $builder->add('name', 'text');
  15. $builder->add('language', 'entity', array(
  16. 'class' => 'LingogoAdminBundle:Language',
  17. 'property' => 'language',
  18. 'empty_value' =>'Please select language',
  19. ));
  20. $builder->add('translations', 'collection', array(
  21. 'type' => new TranslationType(),
  22. 'allow_add' => true,
  23. 'prototype' => true,
  24. 'by_reference' => false,
  25. 'label' => false
  26. ));
  27. $builder->add('definitions', 'collection', array(
  28. 'type' => new DefinitionType(),
  29. 'allow_add' => true,
  30. 'prototype' => true,
  31. 'by_reference' => false,
  32. 'label' => false
  33. ));
  34. $builder->add('synonyms', 'collection', array(
  35. 'type' => new SynonymType(),
  36. 'allow_add' => true,
  37. 'prototype' => true,
  38. 'by_reference' => false,
  39. 'label' => false
  40. ));
  41. $builder->add('add', 'submit');
  42. }
  43.  
  44. public function setDefaultOptions(OptionsResolverInterface $resolver)
  45. {
  46. $resolver->setDefaults(array(
  47. 'data_class'=> 'Lingogo\AdminBundle\Entity\Word',
  48. ));
  49. }
  50.  
  51. /**
  52.   * Returns the name of this type.
  53.   *
  54.   * @return string The name of this type
  55.   */
  56. public function getName()
  57. {
  58. return 'word';
  59. }
  60. }

Teraz, mój problem polega na tym, że gdy renderuje formularz WordType, to każdy element z kolekcji Definition renderuje nadmiarowy przycisk submit.
Czy wiecie może jak pozbyć się nadmiarowych przycisków z DefinitionType? Tak aby wy renderowany został tylko przycisk pochodzący z WordType?

poniżej znajduje się kod, odpowiedzialny za renderowanie formularza:
  1. {{ form_start(form, { 'attr': {'novalidate':'novalidate' }}) }}
  2.  
  3. <div class="row">
  4. <div class="col-md-3">
  5. {{ form_row(form.name) }}
  6. </div>
  7. <div class="col-md-3">
  8. {{ form_row(form.language) }}
  9. </div>
  10. </div>
  11. <p>
  12. <a href="" id="add-another-definition" class="btn btn-success">
  13. <i class="fa fa-plus-square"></i>{% trans %}Add definition{% endtrans %}
  14. </a>
  15. <a href="" id="add-another-translation" class="btn btn-success">
  16. <i class="fa fa-plus-square"></i>{% trans %}Add translation{% endtrans %}
  17. </a>
  18. <a href="" id="add-another-synonym" class="btn btn-success">
  19. <i class="fa fa-plus-square"></i>{% trans %}Add synonym{% endtrans %}
  20. </a>
  21. </p>
  22. <div id="translation-fields-list"
  23. data-prototype="{{ form_widget(form.translations.vars.prototype) | e }}">
  24. </div>
  25. <div id="definition-fields-list"
  26. data-prototype="{{ form_widget(form.definitions.vars.prototype) | e }}">
  27. </div>
  28. <div id="synonym-fields-list"
  29. data-prototype="{{ form_widget(form.synonyms.vars.prototype) | e }}">
  30. </div>
  31. <div id="definitions">
  32. <h4 class="hidden">{% trans %}Definitions{% endtrans %}:</h4>
  33. </div>
  34. <div id="translations">
  35. <h4 class="hidden">{% trans %}Translations{% endtrans %}:</h4>
  36. </div>
  37. <div id="synonyms">
  38. <h4 class="hidden">{% trans %}Synonyms{% endtrans %}:</h4>
  39. </div>
  40.  
  41. <div>{{ form_widget(form.add) }}</div>
  42. {{ form_end(form) }}
amii
Nie sprawdzałem ale może zadziała:
1. Zrób konstruktor w DefinitionType i ustaw właściwiość $marker
  1. public $marker;
  2. public function __construct($marker = false) {
  3. $this->marker = $marker;
  4. parent::__construct();
  5. }

2. Przy tworzeniu buttona z submit w Definition wrzuć go w warunek, który będzie sprawdzał zmienną $marker
3. Na koniec w klasie Word przekaż do konstruktora Definition $marker = true
pabito
hmm, nie wiem czy to dobry pomysł.

Rozwiązałem to w ten sposób:
Z DefinitionType wyrzuciłem pole 'add'. Dzięki temu przy tworzeniu WordType nie mam buttona pochodzącego z definition.

  1. //$builder->add('add', 'submit');


teraz gdy chcę utworzyć sam formularz DefinitionType, to w controller dodaję pole 'add' w prosty sposób.

  1. $form = $this->createForm(new TranslationType(), new Translation());
  2. $form->add('add','submit');


w ten prosty sposób osiągam zamierzony cel, ale nie wiem czy to jest poprawna metoda sad.gif
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.