在与 ViciousAmateur:Default:Index Controller/acction 相关的视图中,我想添加一个表单来过滤分页结果(KnpPaginatorBundle)。在我的控制器中,我创建表单并返回带有表单变量的视图。(啊……如果我将表单直接放在视图中,它会按预期工作)。
/**
* @Route("/{page}", defaults={"page" = 1}, name="homepage")
* @Route("/")
* @Template()
*/
public function indexAction(Request $request, $page)
{
$filters = new Filters();
$form = $this->createForm(new FiltersType(), $filters);
if ($request->isMethod('POST')) {
$form->bind($request);
if ($form->isValid()) {
// Do something with form submited data
}
}
// Do something when index action loads, pagination etc...
return $this->render('ViciousAmateurBundle:Default:index.html.twig', array(
'form' => $form->createView(),
'pagination' => $pagination
)
);
}
但是,如果我尝试将表单放入树枝包含(以使视图继承/分离)将表单变量传递给该视图......那么它就不起作用了。我收到此响应“错误 101 (net::ERR_CONNECTION_RESET):Se ha restablecido la conexión。” (有些词是西班牙语)这是我的观点:
// file: ViciousAmateurBundle:Default:index.html.twig
{% extends '::base.html.twig' %}
{% block body %}
{{ include('ViciousAmateurBundle:Default:filters.html.twig', {'form': form}) }}
{% block filters %}
{% endblock %}
// Stuff in the body
{% endblock %}
如您所见,表单视图是从索引继承(扩展子句)并将代码添加到其“过滤器”树枝块中......
// file: ViciousAmateurBundle:Default:filters.html.twig
{% extends 'ViciousAmateurBundle:default:index.html.twig' %}
{% block filters %}
<section class="filters">
<form action="{{ path('homepage') }}" method="post" {{ form_enctype(form) }} class="filters">
{{ form_errors(form) }}
{{ form_errors(form.country) }}
{{ form_widget(form.country, { 'attr': { 'placeholder': 'País', 'class': 'input-block-level' } }) }}
// Some more form fields...
{{ form_rest(form) }}
<input type="submit" class="filters_submit btn btn-large btn-block" />
</form>
</section>
{% endblock %}
那么......有可能做那件事吗?将表单放入继承的视图(索引中的过滤器视图)中,将控制器给出的表单变量传递给它???它应该工作吗?我错过了什么或做错了什么?谢谢