Symfony 4模态形式
Symfony 4 modal form
我有一个汽车列表,每辆汽车都有一个按钮,按下按钮可以让我访问模态表单来为该特定汽车写票。示例:https://imgur.com/a/t82H9UT
问题是我在将模态和表单集成到我的树枝中时遇到了一些问题。
我要填写的表格总共有 4 个输入项。前 2 个应该由汽车传递的与按下按钮相关的数据预先填充,其余的我可以自己填充。但是我不知道如何将这些信息从树枝传递到表单。
到目前为止我做了什么:
- 我有我的列表,我的按钮在 index.html.twig 文件中,我的表单在一个名为 modal.html.twig 的单独树枝中的模型中,我还制作了一个控制器。
问题:
按下按钮不会显示任何内容。
我的代码:
index.html.twig
{% block title %}Parking index{% endblock %}
{% block body %}
<table id="file_export" class="table table-striped table-bordered">
<tbody>
{% for voitures in voiture %}
<tr>
<td>
{{ voitures.matricule }}
</td>
<td>
{{ voitures.parking.libelle }}
</td>
<td>
<span class="timer" data-expires="{{ voitures.getExpiresAt() }}"></span>
</td>
<td>
<button type="button" class="btn btn-dark" href="{{ path('new_ticket', {'id': voitures.id},{'parking': voitures.parking.id}) }}" data-toggle="modal" data-target="#createmodel" data-whatever="{{ voitures.id }}">ticket</button>
</td>
</tr>
{% endif %}
{% endfor %}
我的modal.html.twig表格:
<div class="modal fade" id="createmodel" tabindex="-1" role="dialog" aria-labelledby="createModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<form>
<div class="modal-header">
<h5 class="modal-title" id="createModalLabel"><i class="ti-marker-alt m-r-10"></i> ticket for:</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
{{ form_start(form) }}
{{ form_row(form.Matricule, { 'label': 'Matricule' }) }}
{{ form_row(form.Parking, { 'label': 'Matricule' }) }}
{{ form_row(form.Date, { 'label': 'Date' }) }}
{{ form_row(form.montant, { 'label': 'Montant' }) }}
<button class="btn">{{ button_label|default('Add') }}</button>
{{ form_end(form) }}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-success"><i class="ti-save"></i> Save</button>
</div>
</form>
</div>
</div>
最后是我的控制器(不起作用)
/**
* @Route("/ticket", name="new_ticket", methods={"GET","POST"})
*/
public function newTicket(Request $request): Response
{
$ticket = new Ticket();
$form = $this->createForm(TicketType::class, $ticket);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->addFlash('success','ammende added !');
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($ticket);
$entityManager->flush();
return $this->redirectToRoute('agent');
}
return $this->render('Agent/modal.html.twig', [
'ticket' => $ticket,
'form' => $form->createView(),
]);
}
这是我找到的解决方案:
我使用 render 将表单集成到我的索引中,它并不完美,但它有效:
{{render(controller('App\Controller\AgentController:newTask')) }}
您的代码中有几处错误。
第一个错误是您在另一个内部声明了两个表单。
{{ form_start(form) }}
打印 <form>
标签。第二个问题是您没有将表格关联到汽车。例如,你应该为每辆车渲染一个模式,并将它传递给你想要的路线 post.
我留个例子
{% for voitures in voiture %}
<tr>
<td>
{{ voitures.matricule }}
</td>
<td>
{{ voitures.parking.libelle }}
</td>
<td>
<span class="timer" data-expires="{{ voitures.getExpiresAt() }}"></span>
</td>
<td>
<button type="button" class="btn btn-dark" href="" data-toggle="modal" data-target="#createmodel" data-whatever="{{ voitures.id }}">ticket</button>
</td>
</tr>
{% set submit_url = path('new_ticket', {'id': voitures.id},{'parking': voitures.parking.id}) %}
{% include 'modal_form.html.twig' wiht {url: submit_url}
{% endfor %}
模态可以是:
<div class="modal fade" id="createmodel" tabindex="-1" role="dialog" aria-labelledby="createModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
{{ form_start(form, {'action': url)}) }}
<div class="modal-header">
<h5 class="modal-title" id="createModalLabel"><i class="ti-marker-alt m-r-10"></i> ticket for:</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
{{ form_row(form.Matricule, { 'label': 'Matricule' }) }}
{{ form_row(form.Parking, { 'label': 'Matricule' }) }}
{{ form_row(form.Date, { 'label': 'Date' }) }}
{{ form_row(form.montant, { 'label': 'Montant' }) }}
<div class="modal-footer">
</div>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-success"><i class="ti-save"></i> Save</button>
</div>
{{ form_end(form) }}
</div>
</div>
</div>
我有一个汽车列表,每辆汽车都有一个按钮,按下按钮可以让我访问模态表单来为该特定汽车写票。示例:https://imgur.com/a/t82H9UT
问题是我在将模态和表单集成到我的树枝中时遇到了一些问题。
我要填写的表格总共有 4 个输入项。前 2 个应该由汽车传递的与按下按钮相关的数据预先填充,其余的我可以自己填充。但是我不知道如何将这些信息从树枝传递到表单。
到目前为止我做了什么:
- 我有我的列表,我的按钮在 index.html.twig 文件中,我的表单在一个名为 modal.html.twig 的单独树枝中的模型中,我还制作了一个控制器。
问题: 按下按钮不会显示任何内容。
我的代码:
index.html.twig
{% block title %}Parking index{% endblock %}
{% block body %}
<table id="file_export" class="table table-striped table-bordered">
<tbody>
{% for voitures in voiture %}
<tr>
<td>
{{ voitures.matricule }}
</td>
<td>
{{ voitures.parking.libelle }}
</td>
<td>
<span class="timer" data-expires="{{ voitures.getExpiresAt() }}"></span>
</td>
<td>
<button type="button" class="btn btn-dark" href="{{ path('new_ticket', {'id': voitures.id},{'parking': voitures.parking.id}) }}" data-toggle="modal" data-target="#createmodel" data-whatever="{{ voitures.id }}">ticket</button>
</td>
</tr>
{% endif %}
{% endfor %}
我的modal.html.twig表格:
<div class="modal fade" id="createmodel" tabindex="-1" role="dialog" aria-labelledby="createModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<form>
<div class="modal-header">
<h5 class="modal-title" id="createModalLabel"><i class="ti-marker-alt m-r-10"></i> ticket for:</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
{{ form_start(form) }}
{{ form_row(form.Matricule, { 'label': 'Matricule' }) }}
{{ form_row(form.Parking, { 'label': 'Matricule' }) }}
{{ form_row(form.Date, { 'label': 'Date' }) }}
{{ form_row(form.montant, { 'label': 'Montant' }) }}
<button class="btn">{{ button_label|default('Add') }}</button>
{{ form_end(form) }}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-success"><i class="ti-save"></i> Save</button>
</div>
</form>
</div>
</div>
最后是我的控制器(不起作用)
/**
* @Route("/ticket", name="new_ticket", methods={"GET","POST"})
*/
public function newTicket(Request $request): Response
{
$ticket = new Ticket();
$form = $this->createForm(TicketType::class, $ticket);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->addFlash('success','ammende added !');
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($ticket);
$entityManager->flush();
return $this->redirectToRoute('agent');
}
return $this->render('Agent/modal.html.twig', [
'ticket' => $ticket,
'form' => $form->createView(),
]);
}
这是我找到的解决方案: 我使用 render 将表单集成到我的索引中,它并不完美,但它有效:
{{render(controller('App\Controller\AgentController:newTask')) }}
您的代码中有几处错误。
第一个错误是您在另一个内部声明了两个表单。
{{ form_start(form) }}
打印 <form>
标签。第二个问题是您没有将表格关联到汽车。例如,你应该为每辆车渲染一个模式,并将它传递给你想要的路线 post.
我留个例子
{% for voitures in voiture %}
<tr>
<td>
{{ voitures.matricule }}
</td>
<td>
{{ voitures.parking.libelle }}
</td>
<td>
<span class="timer" data-expires="{{ voitures.getExpiresAt() }}"></span>
</td>
<td>
<button type="button" class="btn btn-dark" href="" data-toggle="modal" data-target="#createmodel" data-whatever="{{ voitures.id }}">ticket</button>
</td>
</tr>
{% set submit_url = path('new_ticket', {'id': voitures.id},{'parking': voitures.parking.id}) %}
{% include 'modal_form.html.twig' wiht {url: submit_url}
{% endfor %}
模态可以是:
<div class="modal fade" id="createmodel" tabindex="-1" role="dialog" aria-labelledby="createModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
{{ form_start(form, {'action': url)}) }}
<div class="modal-header">
<h5 class="modal-title" id="createModalLabel"><i class="ti-marker-alt m-r-10"></i> ticket for:</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
{{ form_row(form.Matricule, { 'label': 'Matricule' }) }}
{{ form_row(form.Parking, { 'label': 'Matricule' }) }}
{{ form_row(form.Date, { 'label': 'Date' }) }}
{{ form_row(form.montant, { 'label': 'Montant' }) }}
<div class="modal-footer">
</div>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-success"><i class="ti-save"></i> Save</button>
</div>
{{ form_end(form) }}
</div>
</div>
</div>