如何在字段之间添加<br/>

问题描述 投票:0回答:1

我是 Symfony 的新手。 我成功构建了一个表单,但表单字段之间没有空格。 我的表单已显示,但我想在字段之间添加
标签,例如以获得最佳的表单显示效果。 你能告诉我如何实现这一目标吗?

这是我的文件

CategoriesController.php


namespace App\Controller;

...

class CategoriesController extends AbstractController
{
    /**
     * @Route("/categoriesAncien", name="categories")
     */
    public function index(): Response
    {
            $categories = $this->getDoctrine()->getRepository(Categories::class)->findAll();

        return $this->render('categories/categories.html.twig', [
            "form_title" => "Ajouter une catégorie",
            "categories" => $categories,
        ]);
    }

    
    /**
     * @Route("/categories/add", name="add-categorie")
     */
    public function addCategorie(Request $request): Response
    {
        $categorie = new Categories();
        $form = $this->createForm(CategorieFormType::class, $categorie);
        $form->handleRequest($request);
        $nom = $categorie->getNomCategorie();
        $commentaire = $categorie->getCommentaire();

        if($form->isSubmitted() && $form->isValid())
        {
            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->persist($categorie);
            $entityManager->flush();

            return $this->redirect('/categories');
        }

        return $this->render("categories/categorie-form.html.twig", [
            'form_title' => "Ajouter une catégorie",
            'form_categorie' => $form->createView(),
        ]);

        
    }

    ...
}

CategorieFormType.php

<?php

namespace App\Form;

...

class CategorieFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('nomCategorie', TextType::class, [
                'label' => 'Nom de la catégorie',
                'required' => true,
                'attr' => [
                    'placeholder' => 'Saisir un nom',
                    'class' => "form-control"
                ],
                'constraints' => [
                    new NotBlank(['message' => 'Le nom ne peut être vide'])
                ]
            ])
            ->add('commentaire', TextareaType::class, [
                'label' => 'Commentaire',
                'required' => false,
                'attr' => [
                    'placeholder' => 'Saisir un commentaire',
                    'class' => "form-control"
                ]
            ])
            ->add('Enregistrer', SubmitType::class, [
                'label' => 'Enregistrer',
                'attr' => [
                    'class' => "btn btn-primary"
                ]
            ])
            ->getForm();
        ;
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => Categories::class,
        ]);
    }
}

类别.php

<?php

namespace App\Entity;

...

/**
 * @ORM\Entity(repositoryClass=CategoriesRepository::class)
 */
class Categories
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $nomCategorie;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $commentaire;

    /**
     * @ORM\OneToMany(targetEntity=SousCategories::class, mappedBy="categorie_id")
     */
    private $sousCategories;

    public function __construct()
    {
        $this->sousCategories = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getNomCategorie(): ?string
    {
        return $this->nomCategorie;
    }

    public function setNomCategorie(string $nomCategorie): self
    {
        $this->nomCategorie = $nomCategorie;

        return $this;
    }

    public function getCommentaire(): ?string
    {
        return $this->commentaire;
    }

    public function setCommentaire(?string $commentaire): self
    {
        $this->commentaire = $commentaire;

        return $this;
    }

    /**
     * @return Collection|SousCategories[]
     */
    public function getSousCategories(): Collection
    {
        return $this->sousCategories;
    }

    public function addSousCategory(SousCategories $sousCategory): self
    {
        if (!$this->sousCategories->contains($sousCategory)) {
            $this->sousCategories[] = $sousCategory;
            $sousCategory->setCategorieId($this);
        }

        return $this;
    }

    public function removeSousCategory(SousCategories $sousCategory): self
    {
        if ($this->sousCategories->removeElement($sousCategory)) {
            // set the owning side to null (unless already changed)
            if ($sousCategory->getCategorieId() === $this) {
                $sousCategory->setCategorieId(null);
            }
        }

        return $this;
    }
}

CategoriesFormType.php

<?php

namespace App\Form;

...

class CategorieFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('nomCategorie', TextType::class, [
                'label' => 'Nom de la catégorie',
                'required' => true,
                'attr' => [
                    'placeholder' => 'Saisir un nom',
                    'class' => "form-control"
                ],
                'constraints' => [
                    new NotBlank(['message' => 'Le nom ne peut être vide'])
                ]
            ])
            ->add('commentaire', TextareaType::class, [
                'label' => 'Commentaire',
                'required' => false,
                'attr' => [
                    'placeholder' => 'Saisir un commentaire',
                    'class' => "form-control"
                ]
            ])
            ->add('Enregistrer', SubmitType::class, [
                'label' => 'Enregistrer',
                'attr' => [
                    'class' => "btn btn-primary"
                ]
            ])
            ->getForm();
        ;
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => Categories::class,
        ]);
    }
}

categorie-form.html.twig

{% extends "base.html.twig" %}
{% block body %}
<nav class="navbar sticky-top navbar-expand-lg navbar-dark bg-dark">
    <ul class="navbar-nav mr-auto">
        <li class="nav-item">
            <a class="nav-link" href="/api/">Accueil<span class="sr-only">(current)</span></a>
        </li>
        <li class="nav-item">
            <a class="nav-link" href="/depenses">Mes dépenses</a>
        </li>
        <li class="nav-item">
            <a class="nav-link" href="/operations">Mes opérations</a>
        </li>
        <li class="nav-item">
            <a class="nav-link" href="/budgets">Mes budgets</a>
        </li>
        <li class="nav-item">
            <a class="nav-link" href="/api/">API</a>
        </li>
        <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle active" href="/categories/" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                Configuration
            </a>
            <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
                <a class="dropdown-item active" href="/categories/">Gérer les catégories</a>
                <a class="dropdown-item" href="/sous-categories/">Gérer les sous-catégories</a>
            </div>
        </li>
    </ul>
</nav>

<div class="container">
  <br>
  <h5>{{ form_title }}</h5>
  <br>
  <div class="form-group">
    <br/>
    {{ form(form_categorie)|nl2br }}
    <br/>
  </div>
</div>
{% endblock %}

谢谢您的帮助。

php symfony twig bootstrap-5 line-breaks
1个回答
0
投票

使用CSS:

这只是我推荐的替代解决方案,但请尝试在表单上添加 CSS 来创建空格。

例如

margin


使用 HTML:

或者,如果不在行之间放置

<br>
标签,请以这种方式格式化您的分支以创建字段:

{{form_row (form.publicationTime)}}
<br>
{{form_row (form.origin)}}
<br>
{{form_row (form.commission)}}

所以你可以在每行之间放置一个

<br>
(即使我不推荐这种方法)


查看 Symfony 文档

© www.soinside.com 2019 - 2024. All rights reserved.