- Nek,
Hello tout le monde,
J'essaie de valider la suppression d'éléments sur un formulaire de type collection (dynamique). Pour la validation, pas trop de problème (je donne le validator à titre indicatif). Par contre une fois le form réaffiché, il est affiché sans les données supprimées ! (du coup sa re-validation est compromise)
Avez vous une idée de comment régler ce problème ? Je donne des fichiers qui permettront peut être de mieux comprendre le problème.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | <?php class AppointmentController extends Controller { public function editAppointment(Request $request, Appointment $appointment) { // Here // count($appointment->getSlot()) === 3 $form = $this->createForm('appointment', $appointment, [ 'questionnaire' => $questionnaire ]); $form->handleRequest($request); if ($form->isValid()) { // Persisting } // Here on failing validation, there is // count($appointment->getSlot()) === 2 // Because we removed one slot from "dynamically" in the form, but the user can't do that, // so we need to reset the slots but it's not possible because form data is locked after "submit". return $this->render('App:Appointment:edit.html.twig', ['form' => $form->createView()]); } } class AppointmentType extends AbstractTYpe { public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('slots', 'collection', [ 'type' => new SlotType(), 'allow_add' => true, 'prototype' => true, 'allow_delete' => true, 'error_bubbling' => false, 'by_reference' => false, 'constraints' => [ new WrongSlotRemoval($builder->getData()->getSlots()) ] ]) ; } } class WrongSlotRemoval extends Constraint { public $message = 'Impossible to delete slog.'; /** * @var null|\App\Entity\AppointmentSlot[] */ private $slots; /** * @param \App\Entity\AppointmentSlot[]|null $slots */ public function __construct($slots = null) { // Clone the collection because it can be modified by reference // in order to add or delete items. if ($slots !== null) { $this->slots = clone $slots; } } /** * @return \App\Entity\AppointmentSlot[] */ public function getSlots() { return $this->slots; } /** * @param \App\Entity\AppointmentSlot[] $slots * @return self */ public function setSlots($slots) { $this->slots = $slots; return $this; } } class WrongSlotRemovalValidator extends ConstraintValidator { /** * @param \App\Entity\AppointmentSlot[] $object * @param WrongSlotRemoval $constraint */ public function validate($object, Constraint $constraint) { foreach($constraint->getSlots() as $slot) { if (!$object->contains($slot) && !$slot->isDeletable()) { $this->context ->buildViolation($constraint->message) ->addViolation() ; return; } } } } |
Merci d'avance !
[edit] Voici un screen du problème:
+1
-1