• Najnowsze pytania
  • Bez odpowiedzi
  • Zadaj pytanie
  • Kategorie
  • Tagi
  • Zdobyte punkty
  • Ekipa ninja
  • IRC
  • FAQ
  • Regulamin
  • Książki warte uwagi

Problem z dynamicznym formularzem

Cloud VPS
0 głosów
621 wizyt
pytanie zadane 23 grudnia 2017 w PHP przez Kamil Gąsior Obywatel (1,430 p.)

Witam czy pomoglby mi ktos z formularzem ? Probuje go zrobic juz od paru dni, ale nie wychodzi nic :/

Jest to dynamiczny form i po wybraniu dywizji nie wczytuje sie wgl liga :/ Co moze byc nie tak ?

Form:

<?php

namespace App\Form;


use App\Entity\Team;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;


class addMatchType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('division', EntityType::class, array(
                'class'       => 'App\Entity\Team',
                'placeholder' => '',
                'choice_label' => 'division',

            ));
        $formModifier = function (FormInterface $form, Team $team = null) {
            $league = null === $team ? array() : $team->getLeague();
            $form->add('league', EntityType::class, array(
                'class'       => 'App\Entity\Team',
                'placeholder' => '',
                'choices' => $league,
            ));
        };
        $builder->addEventListener(
            FormEvents::PRE_SET_DATA,
            function (FormEvent $event) use ($formModifier) {
                // this would be your entity, i.e. SportMeetup
                $data = $event->getData();
                $formModifier($event->getForm(), $data->getDivision());
            }
        );
        $builder->get('division')->addEventListener(
            FormEvents::POST_SUBMIT,
            function (FormEvent $event) use ($formModifier) {
                // It's important here to fetch $event->getForm()->getData(), as
                // $event->getData() will get you the client data (that is, the ID)
                $team = $event->getForm()->getData();
                // since we've added the listener to the child, we'll have to pass on
                // the parent to the callback functions!
                $formModifier($event->getForm()->getParent(), $team);
            }
        );
    }

}


Twig:


{{ form_start(form) }}
    {{ form_row(form.division) }}    {# <select id="meetup_sport" ... #}
    {{ form_row(form.league) }} {# <select id="meetup_position" ... #}
    {# ... #}
{{ form_end(form) }}

<head>
<script   src="https://code.jquery.com/jquery-2.2.4.min.js"   integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="   crossorigin="anonymous"></script>
<script>
    var $division = $('#add_match_division');
    // When sport gets selected ...
    $division.change(function() {
        // ... retrieve the corresponding form.
        var $form = $(this).closest('form');
        // Simulate form data, but only include the selected sport value.
        var data = {};
        data[$division.attr('division')] = $division.val();
        // Submit data via AJAX to the form's action path.
        $.ajax({
            url : $form.attr('action'),
            type: $form.attr('method'),
            data : data,
            success: function(html) {
                // Replace current position field ...
                $('#add_match_league').replaceWith(
                    // ... with the returned one from the AJAX response.
                    $(html).find('#add_match_league')
                );
                // Position field now displays the appropriate positions.
            }
        });
    });
</script>
</head>

 

Zaloguj lub zarejestruj się, aby odpowiedzieć na to pytanie.

Podobne pytania

0 głosów
1 odpowiedź 265 wizyt
+1 głos
2 odpowiedzi 921 wizyt
+2 głosów
1 odpowiedź 290 wizyt

93,487 zapytań

142,420 odpowiedzi

322,772 komentarzy

62,903 pasjonatów

Motyw:

Akcja Pajacyk

Pajacyk od wielu lat dożywia dzieci. Pomóż klikając w zielony brzuszek na stronie. Dziękujemy! ♡

Oto polecana książka warta uwagi.
Pełną listę książek znajdziesz tutaj

Kursy INF.02 i INF.03
...