Sto costruendo un'app Symfony e utilizzando gli eventi di modulo con alcuni jquery/ajax per fare l'intera cosa "stato/località". Ho un piccolo problema però, sto usando il formato Provincia -> Città -> Periferia. Ora, per quanto posso dire il mio codice va bene, ma quando l'esecuzione colpisce la sezione in cui aggiungo un listener al "City" selezionare, getta un errore che dice quanto segue:Aggiungi listener di eventi per formare l'elemento aggiunto dall'ascoltatore di eventi
The child with the name "physicalCity" does not exist.
Questo, ovviamente, succede quando provo ad aggiungere un listener di eventi al campo appena creato, aggiungendo così un listener di eventi a un elemento creato da un listener di eventi?
Una sezione del codice è sotto ... Cosa sto facendo male? Qualsiasi aiuto sarebbe molto apprezzato!
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('schoolName')
->add('physicalProvince', 'entity', array(
'mapped' => false,
'class' => 'MY\MainBundle\Entity\Province',
'empty_value' => 'Select a province',
'attr' => array(
'class' => 'province',
'data-show' => 'physical-city',
)
));
/*
* For the physical cities
*/
$physicalCityModifier = function(FormInterface $form, Province $province = null) {
if (null !== $province)
$cities = $province->getCities();
else
$cities = array();
$form->add('physicalCity', 'entity', array(
'mapped' => false,
'class' => 'MY\MainBundle\Entity\City',
'empty_value' => 'Select a province first',
'choices' => $cities,
'attr' => array(
'class' => 'city physical-city',
'data-show' => 'physical-suburb'
)
));
};
$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function(FormEvent $event) use ($physicalCityModifier) {
$data = $event->getData();
if (is_object($data->getPhysicalSuburb()))
$province = $data->getPhysicalSuburb()->getCity()->getProvince();
else
$province = null;
$physicalCityModifier($event->getForm(), $province);
}
);
$builder->get('physicalProvince')->addEventListener(
FormEvents::POST_SUBMIT,
function (FormEvent $event) use ($physicalCityModifier) {
$province = $event->getForm()->getData();
$physicalCityModifier($event->getForm()->getParent(), $province);
}
);
/*
* For the physical suburbs
*/
$physicalSuburbModifier = function(FormInterface $form, City $city = null) {
if (null !== $city)
$suburbs = $city->getSuburbs();
else
$suburbs = array();
$form->add('physicalSuburb', null, array(
'choices' => $suburbs,
'empty_value' => 'Select a city first',
'attr' => array(
'class' => 'physical-suburb'
),
));
};
$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function(FormEvent $event) use ($physicalSuburbModifier) {
$data = $event->getData();
if (is_object($data->getCity()))
$city = $data->getCity();
else
$city = null;
$physicalSuburbModifier($event->getForm(), $city);
}
);
$builder->get('physicalCity')->addEventListener(
FormEvents::POST_SUBMIT,
function(FormEvent $event) use ($physicalSuburbModifier) {
$city = $event->getForm()->getData();
$physicalSuburbModifier($event->getForm()->getParent(), $city);
}
);
}
Ottimo lavoro! Hai salvato la mia giornata, grazie! :) – Gianluca78
Funziona anche mentre modifichi questo contenuto. Per me no. Potresti anche rispondere con quale versione di Symfony hai lavorato? – Jeet
Sì, ha funzionato per me. Questo era Sf 2.4 Credo, non ricordo esattamente. Basta provare e giocare con i casi di decisione del generatore di query. Forse c'è qualcosa di strano nel tuo caso. E installa XDebug! Aiuta molto – iLikeBreakfast