将按钮提交转换为图标

时间:2018-05-29 12:51:13

标签: php forms symfony symfony4 symfony-3.4

我有一个向用户显示的项目列表,我希望他有一个小“按钮”(事实上,我更喜欢一个图标)。为了确保我们无法通过循环删除每个文件,我想要一个表单来删除它。

所以,在我的索引操作中,我按项目创建了一个表单:

/**
 * @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');
}

说实话,我不确定我是否遵循了良好的道路。 目前,这是有效的,但我想要一个小的象形图,而不是这个丑陋的输入按钮。 你知道我应该怎么做吗?

如果您发现代码中有错误,请随时注意我。

1 个答案:

答案 0 :(得分:0)

感谢评论,我明白我只需要在视图中直接添加按钮:

{% 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。 一切都按预期工作。

谢谢大家