我需要使用相同的表单进行创建和编辑操作。这是我的表格:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('category', 'entity', array(
'class' => 'IncomingBundle:IncomingCategories',
'property' => 'name',
'expanded' => false,
'multiple' => false
))
->add('sum', 'number', array(
'invalid_message' => 'Wrong format use only numbers'
));
}
这是我的indexAction,我在此处添加一个新的实体并在表中显示实体:
/**
* @Route("/incoming", name="incoming")
* @Template()
* @param Request $request
* @return array
*/
public function indexAction(Request $request)
{
$entities = $this->getIncomingEntities();
$incoming = new Incoming();
$form = $this->createForm(new IncomingFormType(), $incoming);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$user = $this->getUser();
$incoming->setCreateBy($user);
$date = new \DateTime();
$incoming->setCreateAt($date);
$em = $this->getDoctrine()->getManager();
$em->persist($incoming);
$em->flush();
$request->getSession()
->getFlashBag()
->add('success', 'Pomyślnie dodano nowy wpływ');
return $this->redirect($this->generateUrl('incoming'));
}
return array(
'form' => $form->createView(),
'incoming' => $incoming,
'entities' => $entities
);
}
我在表格中显示我的实体,在一列中有编辑和删除操作按钮。
这是我的TWIG:
{% extends "::layout_base.html.twig" %}
{% block title %} Wpływy | Accountant {% endblock %}
{% block content %}
{{ parent() }}
<h2 style="color: #ffc107; margin-bottom: 50px;"><i class="fa fa-arrow-up" style="margin-right: 15px; color: #0A0A0A"></i>Wpływy <small style="color: #0A0A0A">> Lista</small></h2>
<form method="post" action="{{ path('incoming') }}" novalidate="novalidate">
<div class="alert">
{{ form_errors(form) }}
</div>
<div class="row" style="margin-top: 50px; margin-bottom: 50px; text-align: center" >
<div class=".col-md-1"></div>
<div class="col-md-4">
{{ form_row(form.category, {'label': 'Category: '}) }}
</div>
<div class="col-md-4">
{{ form_row(form.sum, {'label': 'Sum: '}) }}
</div>
{{ form_rest(form) }}
<div class="col-md-4">
<input class="btn btn-primary" type="submit" value="Add new"/>
</div>
<div class=".col-md-1"></div>
</div>
</form>
<table class="table table-hover" style="margin-bottom: 40px">
<tr>
<th>Lp</th>
<th>Add date</th>
<th>Add by</th>
<th>Category</th>
<th>Sum</th>
<th>Options</th>
</tr>
{% set countner = 1 %}
{% for entity in entities %}
<tr>
<td>{{ countner }}</td>
<td>{{ entity.createAt.date|date('Y-m-d H:m') }}</td>
<td>{{ entity.createBy.firstName }} {{ entity.createBy.lastName }}</td>
<td>{{ entity.category.name }}</td>
<td>{{ entity.sum|number_format(2, '.', ',') }} zł</td>
<td>
<a href="" title="Edit" style="margin-right: 5px;"><i class="fa fa-edit" style="font-size: 20px; color: blue;"></i></a>
<a href="{{ path('deleteIncoming', { 'incoming': entity.id }) }}" title="Delete" style="margin-right: 5px;"><i class="fa fa-remove" style="font-size: 20px; color: red;"></i></a>
</td>
</tr>
{% set countner = countner + 1 %}
{% endfor %}
</table>
{% endblock %}
我想使用相同的表格来创建和编辑。如何创建编辑动作? 我要在表格选项中单击按钮“编辑”,然后将所选的实体加载到我的表单中,并更改按钮名称以进行更新。按下按钮后,更新实体将被编辑。
感谢大家的帮助。