У меня проблема с формой проверки, когда я отправляю сообщение об ошибке, просто обновите sh страницу, и я не понимаю, что я забыл? Я пытался с SubmitType, но у меня та же проблема.
My FormType
class FilmType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('titre')
->add('resume')
->add('dateSortie')
->add('personnages', EntityType::class, [
'class' => Personnage::class,
'query_builder' => function (PersonnageRepository $pr) {
return $pr->createQueryBuilder('p')
->orderBy('p.nom', 'ASC');
},
'choice_label' => 'nom'
])
->getForm();
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Film::class,
]);
}
}
Мой контроллер, я думаю, в порядке
/**
*
* @param Request $request
* @Route("/film/new", name="film_new")
* @return Response
*
*/
public function new(Request $request): Response
{
$film = new Film();
$form = $this->createForm(FilmType::class, $film);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($film);
$entityManager->flush();
return $this->redirectToRoute('home');
}
return $this->render('film/new.html.twig', [
'film' => $film,
'form' => $form->createView(),
]);
}
МОЯ веточка, я добавляю form_errors, но не более
{% block body %}
<form>
<div class="form-group">
{{ form_start(form)}}
{{ form_errors(form) }}
{{ form_widget(form) }}
<button> Envoyer </button>
{{ form_end(form) }}
</div>
</form>
{% endblock %}
My Film entity .....
/**
* @ORM\Entity(repositoryClass="App\Repository\FilmRepository")
*/
class Film
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $titre;
/**
* @ORM\Column(type="text")
*/
private $resume;
/**
* @ORM\Column(type="date")
*/
private $dateSortie;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Personnage", mappedBy="film")
*/
private $personnages;
public function __construct()
{
$this->personnages = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getTitre(): ?string
{
return $this->titre;
}
public function setTitre(string $titre): self
{
$this->titre = $titre;
return $this;
}
public function getResume(): ?string
{
return $this->resume;
}
public function setResume(string $resume): self
{
$this->resume = $resume;
return $this;
}
public function getDateSortie(): ?\DateTimeInterface
{
return $this->dateSortie;
}
public function setDateSortie(\DateTimeInterface $dateSortie): self
{
$this->dateSortie = $dateSortie;
return $this;
}
/**
* @return Collection|Personnage[]
*/
public function getPersonnages(): Collection
{
return $this->personnages;
}
public function addPersonnage(Personnage $personnage): self
{
if (!$this->personnages->contains($personnage)) {
$this->personnages[] = $personnage;
$personnage->addFilm($this);
}
return $this;
}
public function removePersonnage(Personnage $personnage): self
{
if ($this->personnages->contains($personnage)) {
$this->personnages->removeElement($personnage);
$personnage->removeFilm($this);
}
return $this;
}