我正在使用symfony 2.8和php 7,并且确实遇到了使用地理数据填写表单的问题(国家/州/省/市/邮编)。
我需要的是5个选择框,第一个已经填充,其他人在我更改之前选择后重新加载。例如,如果我更改国家/地区,则州选择将填充新值。
我今天读了很多例子,但仍然不知道如何运行它。
这是我的代码:
如果有5个返回数组的方法,你可以通过它的名字来理解它们返回的内容:
private function getCountryList()
private function getStatesList( $idCountry )
private function getProvinceList( $idState )
private function getCityList( $idProvince )
private function getZipList( $idCity )
这是我的表格:
$locationForm = $builder->create('location', FormType::class, array('data_class' => 'M3\CoreBundle\Entity\Location',
'by_reference' => true));
$locationForm->add('GeoCountryId', ChoiceType::class, array(
'label' => 'Country',
'choices' => $this->getCountryList(),
));
$locationForm->add('GeoStateId', ChoiceType::class, array('label' => 'State', 'choices' => array()));
$locationForm->add('GeoProvinceId', ChoiceType::class, array('label' => 'Province', 'choices' => array()));
$locationForm->add('GeoCityId', ChoiceType::class, array('label' => 'City', 'choices' => array()));
$locationForm->add('GeoZipCodeId', ChoiceType::class, array('label' => 'Zip Code', 'choices' => array()));
我尝试添加一些像这样的事件
$locationForm->addEventListener(
FormEvents::PRE_SET_DATA,
function (FormEvent $event) {
$data = $event->getData();
$form = $event->getForm();
$country = $form->get('GeoCountryId')->getData();
//here i tried a lot of things
$form->get('GeoStateId')->setData($this->getStatesList($country));
}
);
我尝试了很多东西,却一无所获。我阅读了本手册http://symfony.com/doc/2.8/form/dynamic_form_modification.html#form-events-user-data但也没有任何作用。你能救我吗?
也许我只需要添加javascript事件" on change"用于手动选择和加载所有数据?
答案 0 :(得分:0)
您必须使用PHP和JS的组合。您可以使用下面几段文档的示例:http://symfony.com/doc/2.8/form/dynamic_form_modification.html#dynamic-generation-for-submitted-forms
{# app/Resources/views/Meetup/create.html.twig #}
{{ form_start(form) }}
{{ form_row(form.sport) }} {# <select id="meetup_sport" ... #}
{{ form_row(form.position) }} {# <select id="meetup_position" ... #}
{# ... #}
{{ form_end(form) }}
<script>
var $sport = $('#meetup_sport');
// When sport gets selected ...
$sport.change(function() {
// ... retrieve the corresponding form.
var $form = $(this).closest('form');
// Simulate form data, but only include the selected sport value.
var data = {};
data[$sport.attr('name')] = $sport.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 ...
$('#meetup_position').replaceWith(
// ... with the returned one from the AJAX response.
$(html).find('#meetup_position')
);
// Position field now displays the appropriate positions.
}
});
});
</script>
答案 1 :(得分:0)
树枝页面:
//继续
{{form_widget(form.matiere)}}
///
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$( "select[name='continue[matiere]']" ).change(function () {
var matiereID = $(this).val();
if(matiereID) {
$.ajax({
url: "{{ path('ajaxform') }}",
dataType: 'Json',
data: {'id':matiereID},
type:'POST',
success: function(data) {
$('select[name="continue[chapitre]"]').empty();
for(i = 0; i < data.length; i++) {
student = data[i];
$('select[name="continue[chapitre]"]').append('<option value="'+ student['id'] +'">'+ student['nom'] +'</option>');
};
}
});
}else{
$('select[name="continue[chapitre]"]').empty();
}
});
</script>
形式(例如:continueType):
->add('classe',EntityType::class,array(
'class'=> Classe::class,
'choice_label'=>'nom',
'placeholder'=>'choisir une classe'));
$builder->addEventListener(FormEvents::PRE_SET_DATA, array($this, 'onPreSetData'));
$builder->addEventListener(FormEvents::PRE_SUBMIT, array($this, 'onPreSubmit'));
}
protected function addElements(FormInterface $form, matiere $matiere = null) {
// 4. Add the province element
$form->add('matiere', EntityType::class, array(
'required' => true,
'data' => $matiere,
'placeholder' => 'Select a matiere...',
'class' => Matiere::class
));
// chapitres empty, unless there is a selected matiere (Edit View)
$chapitres = array();
// If there is a matiere stored in the Person entity, load the chapitres of it
if ($matiere) {
// Fetch chapitres of the matiere if there's a selected matiere
$repochapitres = $this->em->getRepository(Chapitre::class);
$chapitres = $repochapitres->createQueryBuilder("q")
->where("q.matiere = :matiereid")
->setParameter("matiereid", $matiere->getId())
->getQuery()
->getResult();
}
// Add the chapitres field with the properly data
$form->add('chapitre', EntityType::class, array(
'required' => true,
'placeholder' => 'Select a matiere first ...',
'class' => Chapitre::class,
'choices' => $chapitres
));
}
function onPreSubmit(FormEvent $event) {
$form = $event->getForm();
$data = $event->getData();
// Search for selected matiere and convert it into an Entity
$matiere = $this->em->getRepository(Matiere::class)->find($data['matiere']);
$this->addElements($form, $matiere);
}
function onPreSetData(FormEvent $event) {
$contenue = $event->getData();
$form = $event->getForm();
// When you create a new person, the matiere is always empty
$matiere = $contenue->getMatiere() ? $contenue->getMatiere() : null;
$this->addElements($form, $matiere);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Contenue::class,
]);
}
public function getBlockPrefix()
{
return 'continue';
}
}
控制器中的/ ** * @Route(“ / exercice / ajax”,name =“ ajaxform”) * / 公共功能ajaxAction(Request $ request){
$responseArray[] = array();
if ($request->isXmlHttpRequest() ) {
$id= $_POST['id'] ;
$matriere = $this->getDoctrine()
->getRepository(Matiere::class)
->find($id);
$chapitre=$matriere->getChapitres();
foreach($chapitre as $chapitres) {
$responseArray[] = array(
'id' => $chapitres->getId(),
'nom' => $chapitres-> getNom(),
);
}
}
return new JsonResponse($responseArray);
}