我正尝试创建搜索表单。在这个表格中,我有两个选择框,一个用于国家,另一个用于名为“目的地”的城市。第二个取决于选定的国家。我也有其他一些投入。提交表单后无法恢复一个数据
选择国家后,使用ajax查询正确显示与其相关的所有城市。
问题是如果我选择一个国家并提交表单,目的地不会在地址栏中恢复,但如果我不选择国家/地区,目的地将会恢复。
这是我听不懂没有选择国家
/q?title=&country=&destination=&airport=&departureDate=&returnDate=&price=
地址栏的显示,这是,如果我选择一个国家的地址栏的显示方式。正如你所看到的目标是缺少
/q?title=&country=1&airport=&departureDate=&returnDate=&price=
注意我使用的是类似的形式与POST方法来创建或编辑新的旅行和正常工作
SearchTravelType
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
//........
use Symfony\Component\Form\FormInterface;
use AppBundle\Entity\Country;
class SearchTravelType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
->add('country', 'entity', array(
'required' => true,
'class' => 'AppBundle:Country',
'property' => 'en',
'multiple' => false,
'expanded' => false,
'label' => 'ِCountry',
'empty_value' => ''))
//....
$formModifier = function (FormInterface $form, Country $country = null) {
$cities = null === $country ? array() : $country->getCities();
$form->add('destination', 'entity', array(
'class' => 'AppBundle:CityWorld',
'choices' => $cities,
'multiple' => false,
'expanded' => false,
'property' => 'name',
'label' => 'Destination'));
};
$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function (FormEvent $event) use ($formModifier) {
// this would be your entity
$data = $event->getData();
$formModifier($event->getForm(), $data->getCountry());
}
);
$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)
$country = $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(), $country);
}
);
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Travel'
));
}
public function getName()
{
return null;
}
}
这是表单和JavaScript代码
<form method="get" action="" class="form-horizontal" role="form" >
{{ form_widget(form.country, { 'id': 'country' }) }}
{{ form_widget(form.destination, { 'id': 'city' }) }}
//.............
</form>
<script type="text/javascript">
var $county = $('#country');
$county.change(function() {
// ... retrieve the corresponding form.
var $form = $(this).closest('form');
var data = {};
data[$county.attr('name')] = $county.val();
// Submit data via AJAX to the form's action path.
$.ajax({
url: '{{ path('dashboard_travel_search', {'agence_slug': agence.slug}) }}',
type: 'POST', data: data,
success: function (html) {
$('#city').replaceWith(
// ... with the returned one from the AJAX response.
$(html).find('#city')
);
}
});
});
这是控制器
/**
* @ParamConverter("agence", options={"mapping": {"agence_slug":"slug"}})
*/
public function searchAction(Request $request, Agence $agence)
{
if (false === $this->get('security.context')->isGranted('view', $agence)) {
throw new AccessDeniedException('Unauthorised access!');
}
/* Instantiate Travel to not get this error
Error: Call to a member function getCountry() on a non-object
*/
$travel = new Travel();
$form = $this->createForm(new SearchTravelType(), $travel);
if ($request->getMethod() == 'POST') {
$form->handleRequest($request);
}
$em = $this->getDoctrine()->getManager();
$paginator = $this->get('knp_paginator');
$title = $request->query->get('title');
$country = $request->query->get('country');
$destination = $request->query->get('destination');
$price = $request->query->get('price');
$departureDate = $request->query->get('departureDate');
$returnDate = $request->query->get('returnDate');
$nbDays = $request->query->get('nbDays');
$nbNights = $request->query->get('nbNights');
$airport = $request->query->get('airport');
$enabled = $request->query->get('enabled');
$qb = $em->getRepository('AppBundle:Travel')->getListSearchTravelsDashboard($agence, $title, $country, $destination, $price, $departureDate, $returnDate, $nbDays, $nbNights, $airport, $enabled);
$pagination = $paginator->paginate(
$qb,
$request->query->get('page', 1), 10);
$pagination->setSortableTemplate('KnpPaginatorBundle:Pagination:sortable_link_dashboard.html.twig');
return $this->render('AppBundle:Dashboard/Travel:list-search.html.twig', array(
'pagination' => $pagination,
'form' => $form->createView(),
'agence' => $agence,
));
}
这是生成的表单的嫩枝HTML标记:
<table id="sample-table-1" class="table table-responsives table-striped table-hover">
<tr>
<form class="form-inline" role="form" method="get" action="">
<td class="center">-</td>
<td>{{ form_widget(form.title) }}</td>
<td>{{ form_widget(form.country, { 'id': 'country' }) }}</td>
<td>{{ form_widget(form.destination, { 'id': 'city' }) }}</td>
<td>{{ form_widget(form.airport) }}</td>
<td>{{ form_widget(form.departureDate) }}</td>
<td>{{ form_widget(form.returnDate) }}</td>
<td>{{ form_widget(form.price) }}</td>
<td>{{ form_widget(form.nbDays) }}</td>
<td>{{ form_widget(form.nbNights) }}</td>
<td>{{ form_widget(form.enabled) }} {{ form_rest(form) }}</td>
<td><span class="input-group-btn">
<button type="submit" class="btn btn-purple btn-sm" title="Rechercher">
<i class="icon-search bigger-110"></i></button>
</span>
</form>
</tr>
我已经把它在表外,现在工作得很好。有什么解释吗?
您是否在使用AJAX显示相关城市?如果是这样,请提供用于显示相关城市的JavaScript。在选择城市之后,请检查您是否可以使用JavaScript接收选定的选项。如果您无法通过JavaScript接收选中的选择选项,请检查在DOM中选择城市后脚本将发生的变化。它可以是,它只会改变一个类或属性来显示选择内容,但是它不会检查选项(例如使用'checked'属性)。 – dude 2015-02-23 22:36:53
@julmot,我使用Ajax JavaScript根据国家/地区字段中的当前选择更新目的地表单字段,并使用EVENTS选择城市。以下是文档中的示例。 (http://symfony.com/doc/cookbook/form/dynamic_form_modification.html#cookbook-form-events-submitted-data) – hous 2015-02-24 10:28:24