交响乐:在树枝文件中的字段之间添加 <br/>
Posted
技术标签:
【中文标题】交响乐:在树枝文件中的字段之间添加 <br/>【英文标题】:symphony : Add <br/> between fields in twig file 【发布时间】:2021-12-20 13:13:18 【问题描述】:我是 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,
]);
Categories.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 %
感谢您的帮助。
【问题讨论】:
你能分享你正在使用的 Twig 模板吗?为什么不编辑它并在需要的地方添加标签? 您好,谢谢。我用树枝文件编辑了我的帖子。你能告诉我如何更新吗? 【参考方案1】:使用 CSS:
这只是我推荐的替代解决方案,但请尝试在表单上添加 CSS 以创建空间。
例如margin
使用 HTML:
或者,如果不在行之间放置<br>
标签,请以这种方式格式化您的分支以制作字段:
form_row (form.publicationTime)
<br>
form_row (form.origin)
<br>
form_row (form.commission)
所以你可以在每行之间放一个<br>
(即使我不推荐这种方法)
查看 Symfony documentation
【讨论】:
以上是关于交响乐:在树枝文件中的字段之间添加 <br/>的主要内容,如果未能解决你的问题,请参考以下文章
EasyAdmin:更改树枝中的 formTypeOptions 属性字段时出错