【发布时间】:2018-01-29 15:13:11
【问题描述】:
我有 3 个实体工作正常,但创建 symfony 表单时出现问题
// My FormType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$translator = $this->translator;
$builder
->add('Country', EntityType::class, array(
'class' => 'App\Entity\Countries',
'placeholder' => '',
))
->add('City', EntityType::class,array(
'class' =>'App\Entity\Cities'
))
;
$formModifier = function (FormInterface $form, Countries $sport = null) {
$positions = null === $sport ? array() : $sport->getCities();
$form->add('City', EntityType::class, array(
'class' => 'App\Entity\Cities',
'placeholder' => '',
'choices' => $positions,
));
};
$formModifier2 = function (FormInterface $form, Cities $sport = null) {
$positions = null === $sport ? array() : $sport->getCountryId();
$form->add('District', EntityType::class, array(
'class' => 'App\Entity\Districts',
'placeholder' => '',
'choices' => $positions,
));
};
$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->getCountry());
}
);
$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function (FormEvent $event) use ($formModifier2) {
// this would be your entity, i.e. SportMeetup
$data = $event->getData();
$formModifier2($event->getForm(), $data->getCity());
}
);
$builder->get('Country')->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)
$sport = $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(), $sport);
}
);
$builder->get('City')->addEventListener(
FormEvents::POST_SUBMIT,
function (FormEvent $event) use ($formModifier2) {
// It's important here to fetch $event->getForm()->getData(), as
// $event->getData() will get you the client data (that is, the ID)
$sport = $event->getForm()->getData();
// since we've added the listener to the child, we'll have to pass on
// since we've added the listener to the child, we'll have to pass on
// the parent to the callback functions!
$formModifier2($event->getForm()->getParent()->getParent(), $sport);
}
);
}
// this my controller
public function userAddress(UserInterface $user,Request $request,HelperService $helper, MailGenerator $mail,TranslatorInterface $translator)
{
$user_address = new UserAddresses();
$form = $this->createForm(NewAddressFormType::class,$user_address,array('csrf_protection' => false));
$form->handleRequest($request);
if($form->isSubmitted()) {
$data = $form->getData();
}
return $this->output('checkout/checkout_address',array('form'=>$form->createView()));
}
Form rendering ok i change my first select box and working ajax post but second one first but third one is not working not send ajax data
{% block script%}
<script>
var $sport = $('#new_address_form_Country');
// When sport gets selected ...
$sport.change(function() {
// ... retrieve the corresponding form.
var $form = $('#new_address_form').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 ...
$('#new_address_form_City').replaceWith(
// ... with the returned one from the AJAX response.
$(html).find('#new_address_form_City')
);
}
});
});
var $sport2 = $('#new_address_form_City');
// When sport gets selected ...
$sport2.change(function() {
// ... retrieve the corresponding form.
var $form = $('#new_address_form').closest('form');
// Simulate form data, but only include the selected sport value.
var data = {};
data[$sport2.attr('name')] = $sport2.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 ...
$('#new_address_form_District').replaceWith(
// ... with the returned one from the AJAX response.
$(html).find('#new_address_form_District')
);
}
});
});
</script>
{% endblock %}
【问题讨论】: