我在交响乐3.2.4中,我有3个实体:国家 - 省 - 城市。我想要动态填充3个字段。
我解释一下,我有3个字段:Country>省>城市,用户选择一个国家,省名单更新,他选择一个省,城市列表更新。
但是我有一个问题,因为只有一个字段动态更新,它是有效的,但是对于2个字段没有...实际上用户可以选择一个国家,当他这样做时,省字段会更新。但是,当他选择一个省时,城市领域没有更新......
这是我的代码:
# FormType
...
$builder->addEventSubscriber(new DynamicFieldsSubscriber());
...
# DynamicFieldsSubscriber
class DynamicFieldsSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return array(
FormEvents::PRE_SET_DATA => 'preSetData',
FormEvents::PRE_SUBMIT => 'preSubmitData'
);
}
public function preSetData(FormEvent $event)
{
$location = $event->getData();
$form = $event->getForm();
$country = "";
$province = "";
$city = "";
if ($location) {
// collect preliminary values for 3 fields.
$country = $location->getEditeur();
$prov
ince = $location->getProvince();
$city = $location->getCity();
}
// Add country field as its static.
$form
->add('country', EntityType::class, array(
'class' => Editeur::class,
'choice_label' => 'country',
'label' => false,
'required' => true,
'placeholder' => 'Éditeur *',
'empty_data' => 'Éditeur *',
'attr' => array(
'class' => 'col-md-12 validate-required',
'placeholder' => 'Éditeur *'
)
))
;
// Now add all child fields.
$this->addProvinceField($form, $country);
$this->addCityField($form, $province);
}
/**
* Handling Form fields before form submits.
* @param FormEvent $event
*/
public function preSubmitData(FormEvent $event)
{
$form = $event->getForm();
$data = $event->getData();
// Here $data will be in array format.
// Add property field if parent entity data is available.
$country = isset($data['country']) ? $data['country'] : $data['country'];
$province = isset($data['province']) ? $data['province'] : null;
$city = isset($data['city']) ? $data['city'] : null;
// Call methods to add child fields.
$this->addProvinceField($form, $country);
$this->addCityField($form, $province);
}
/**
* Method to Add State Field. (first dynamic field.)
* @param FormInterface $form
* @param type $country
*/
private function addProvinceField(FormInterface $form, $country = null)
{
$countryCode = (is_object($country)) ? $country->getId() : $country;
// $countryCode is dynamic here, collected from the event based data flow.
$form
->add('province', EntityType::class, array(
'class' => Province::class,
'choice_label' => 'province',
'label' => false,
'required' => true,
'placeholder' => 'Province *',
'empty_data' => 'Province *',
'attr' => array(
'class' => 'col-md-12 validate-required',
'placeholder' => 'Province *'
),
'query_builder' => function (EntityRepository $er) use($countryCode) {
return $er->createQueryBuilder('p')
->where('p.country = :country')
->setParameter('country', $countryCode);
}
))
;
}
/**
* Method to Add District Field, (second dynamic field)
* @param FormInterface $form
* @param type $state
*/
private function addCityField(FormInterface $form, $province = null)
{
$provinceCode = (is_object($province)) ? $province->getId() : $province;
// $stateCode is dynamic in here collected from event based data flow.
$form->add('city', EntityType::class, array(
'class' => City::class,
'choice_label' => 'city',
'label' => false,
'required' => true,
'placeholder' => 'City *',
'empty_data' => 'City *',
'attr' => array(
'class' => 'col-md-12 validate-required',
'placeholder' => 'City *'
),
'query_builder' => function (EntityRepository $er) use($provinceCode) {
return $er->createQueryBuilder('e')
->where('e.province = :province')
->setParameter('province', $provinceCode);
}
));
}
}
## jQuery
var $country = $('#ec_country');
$country('change', function() {
var $form = $(this).closest('form');
var data = {};
data[$country('name')] = $country();
$.ajax({
url : $form.attr('action'),
type: $form.attr('method'),
data : data,
success: function(html) {
$('#ec_province').replaceWith(
$(html).find('#ec_province')
);
}
});
});
var $province = $('#ec_province');
$(document).on('change', $province, function() {
var $form = $(this).closest('form');
var data = {};
data[$programme.attr('name')] = $('#ec_province').val();
$.ajax({
url : $form.attr('action'),
type: $form.attr('method'),
data : data,
success: function(html) {
$('#ec_city').replaceWith(
$(html).find('#ec_city')
);
}
});
});
任何想法?感谢