Mam taki problem.
Mam poniższą klasę formularza: DefinitionType. W tym formularzu jest przycisk submit
<?php namespace Lingogo\AdminBundle\Form\Type; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; class DefinitionType extends AbstractType { { 'class' => 'LingogoAdminBundle:PartOfSpeech', 'property' => 'name', 'empty_value' => 'Please select part of speech' )); $builder->add('definition', 'textarea'); $builder->add('exampleUsage', 'textarea'); $builder->add('add', 'submit'); } public function setDefaultOptions(OptionsResolverInterface $resolver){ 'data_class' => 'Lingogo\AdminBundle\Entity\Definition' )); } /** * Returns the name of this type. * * @return string The name of this type */ public function getName() { return 'definition'; } }
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
<?php //src/Lingogo/AdminBundle/Form/WordType.php namespace Lingogo\AdminBundle\Form\Type; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; class WordType extends AbstractType { { $builder->add('name', 'text'); 'class' => 'LingogoAdminBundle:Language', 'property' => 'language', 'empty_value' =>'Please select language', )); 'type' => new TranslationType(), 'allow_add' => true, 'prototype' => true, 'by_reference' => false, 'label' => false )); 'type' => new DefinitionType(), 'allow_add' => true, 'prototype' => true, 'by_reference' => false, 'label' => false )); 'type' => new SynonymType(), 'allow_add' => true, 'prototype' => true, 'by_reference' => false, 'label' => false )); $builder->add('add', 'submit'); } public function setDefaultOptions(OptionsResolverInterface $resolver) { 'data_class'=> 'Lingogo\AdminBundle\Entity\Word', )); } /** * Returns the name of this type. * * @return string The name of this type */ public function getName() { return 'word'; } }
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:
{{ form_start(form, { 'attr': {'novalidate':'novalidate' }}) }} <div class="row"> <div class="col-md-3"> {{ form_row(form.name) }} </div> <div class="col-md-3"> {{ form_row(form.language) }} </div> </div> <p> <a href="" id="add-another-definition" class="btn btn-success"> <i class="fa fa-plus-square"></i>{% trans %}Add definition{% endtrans %} </a> <a href="" id="add-another-translation" class="btn btn-success"> <i class="fa fa-plus-square"></i>{% trans %}Add translation{% endtrans %} </a> <a href="" id="add-another-synonym" class="btn btn-success"> <i class="fa fa-plus-square"></i>{% trans %}Add synonym{% endtrans %} </a> </p> <div id="translation-fields-list" data-prototype="{{ form_widget(form.translations.vars.prototype) | e }}"> </div> <div id="definition-fields-list" data-prototype="{{ form_widget(form.definitions.vars.prototype) | e }}"> </div> <div id="synonym-fields-list" data-prototype="{{ form_widget(form.synonyms.vars.prototype) | e }}"> </div> <div id="definitions"> <h4 class="hidden">{% trans %}Definitions{% endtrans %}:</h4> </div> <div id="translations"> <h4 class="hidden">{% trans %}Translations{% endtrans %}:</h4> </div> <div id="synonyms"> <h4 class="hidden">{% trans %}Synonyms{% endtrans %}:</h4> </div> <div>{{ form_widget(form.add) }}</div> {{ form_end(form) }}