将提交按钮转换为图标
Transform a button submit into an icon
我有一个显示给用户的项目列表,我希望他有一个小 "button"(事实上,我更喜欢图标)。为了确保我们不能通过循环删除每个文件,我想要一个表格来删除它。
因此,在我的索引操作中,我按项目创建了一个表单:
/**
* @Route("/files", name="files")
* @return \Symfony\Component\HttpFoundation\Response
*/
public function files() {
$repository = $this->getDoctrine()->getRepository("App:Document");
$files = $repository->findBy([], ["createdAt" => "DESC"]);
$forms = [];
foreach ($files as $file) {
$forms[$file->getId()] = $this->createForm(DocumentRemoveType::class, $file)->createView();
}
return $this->render("admin/files.html.twig", [
"files" => $files,
"forms" => $forms
]);
}
然后,在我的树枝视图中,我显示每种形式:
{% for file in files %}
{# @var file \App\Entity\Document #}
<tr>
<th scope="row">{{ file.name }}</th>
<td>{{ file.createdAt|date("d/m/Y") }}</td>
<td class="text-center">
<a href="{{ vich_uploader_asset(file, 'file') }}" target="_blank" title="Télécharger le document"><i class="fa fa-download"></i></a>
{{ form_start(forms[file.id], {attr: {class: 'my-form-class'} }) }}
{{ form_widget(forms[file.id]) }}
{{ form_end(forms[file.id]) }}
</td>
</tr>
{% endfor %}
然后,这是 DocumentRemoveType
class DocumentRemoveType extends AbstractType
{
private $urlGenerator;
public function __construct(UrlGeneratorInterface $urlGenerator)
{
$this->urlGenerator = $urlGenerator;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add("id", HiddenType::class)
->setAction($this->urlGenerator->generate("admin_files_deletion"))
->add("removeButton", SubmitType::class);
}
}
最后,这是删除操作:
/**
* @Route("/files/delete", name="admin_files_deletion")
* @param Request $request
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
public function filesDeletion(Request $request) {
$form = $this->createForm(DocumentRemoveType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$manager = $this->getDoctrine()->getManager();
$file = $manager->find(Document::class, $manager["id"]);
$manager->remove($file);
$manager->flush();
}
return $this->redirectToRoute('admin_files');
}
老实说,我不太确定自己是否走上了正确的道路。
目前,这可行,但我想要一个小象形图而不是这个丑陋的输入按钮。
你知道我该怎么做吗?
如果您发现代码中有问题,请随时通知我。
感谢评论,我知道我只需要直接在视图中添加按钮即可:
{% for file in files %}
{# @var file \App\Entity\Document #}
<tr>
<th scope="row">{{ file.name }}</th>
<td>{{ file.createdAt|date("d/m/Y") }}</td>
<td class="text-center">
{{ form_start(forms[file.id], { "attr" : { "onsubmit" : "return confirm('Êtes-vous certain de supprimer ce document?');"}}) }}
<a href="{{ vich_uploader_asset(file, 'file') }}" target="_blank" title="Télécharger le document"><i class="fa fa-download"></i></a>
<button type="submit" class="buttonAsLink"><i class="fa fa-close"></i></button>
{{ form_widget(forms[file.id]) }}
{{ form_end(forms[file.id]) }}
</td>
</tr>
{% endfor %}
并且我删除了表单中的 SubmitType。
现在一切正常。
谢谢大家
我有一个显示给用户的项目列表,我希望他有一个小 "button"(事实上,我更喜欢图标)。为了确保我们不能通过循环删除每个文件,我想要一个表格来删除它。
因此,在我的索引操作中,我按项目创建了一个表单:
/**
* @Route("/files", name="files")
* @return \Symfony\Component\HttpFoundation\Response
*/
public function files() {
$repository = $this->getDoctrine()->getRepository("App:Document");
$files = $repository->findBy([], ["createdAt" => "DESC"]);
$forms = [];
foreach ($files as $file) {
$forms[$file->getId()] = $this->createForm(DocumentRemoveType::class, $file)->createView();
}
return $this->render("admin/files.html.twig", [
"files" => $files,
"forms" => $forms
]);
}
然后,在我的树枝视图中,我显示每种形式:
{% for file in files %}
{# @var file \App\Entity\Document #}
<tr>
<th scope="row">{{ file.name }}</th>
<td>{{ file.createdAt|date("d/m/Y") }}</td>
<td class="text-center">
<a href="{{ vich_uploader_asset(file, 'file') }}" target="_blank" title="Télécharger le document"><i class="fa fa-download"></i></a>
{{ form_start(forms[file.id], {attr: {class: 'my-form-class'} }) }}
{{ form_widget(forms[file.id]) }}
{{ form_end(forms[file.id]) }}
</td>
</tr>
{% endfor %}
然后,这是 DocumentRemoveType
class DocumentRemoveType extends AbstractType
{
private $urlGenerator;
public function __construct(UrlGeneratorInterface $urlGenerator)
{
$this->urlGenerator = $urlGenerator;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add("id", HiddenType::class)
->setAction($this->urlGenerator->generate("admin_files_deletion"))
->add("removeButton", SubmitType::class);
}
}
最后,这是删除操作:
/**
* @Route("/files/delete", name="admin_files_deletion")
* @param Request $request
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
public function filesDeletion(Request $request) {
$form = $this->createForm(DocumentRemoveType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$manager = $this->getDoctrine()->getManager();
$file = $manager->find(Document::class, $manager["id"]);
$manager->remove($file);
$manager->flush();
}
return $this->redirectToRoute('admin_files');
}
老实说,我不太确定自己是否走上了正确的道路。 目前,这可行,但我想要一个小象形图而不是这个丑陋的输入按钮。 你知道我该怎么做吗?
如果您发现代码中有问题,请随时通知我。
感谢评论,我知道我只需要直接在视图中添加按钮即可:
{% for file in files %}
{# @var file \App\Entity\Document #}
<tr>
<th scope="row">{{ file.name }}</th>
<td>{{ file.createdAt|date("d/m/Y") }}</td>
<td class="text-center">
{{ form_start(forms[file.id], { "attr" : { "onsubmit" : "return confirm('Êtes-vous certain de supprimer ce document?');"}}) }}
<a href="{{ vich_uploader_asset(file, 'file') }}" target="_blank" title="Télécharger le document"><i class="fa fa-download"></i></a>
<button type="submit" class="buttonAsLink"><i class="fa fa-close"></i></button>
{{ form_widget(forms[file.id]) }}
{{ form_end(forms[file.id]) }}
</td>
</tr>
{% endfor %}
并且我删除了表单中的 SubmitType。 现在一切正常。
谢谢大家