How can I avoid to create a duplicate in my form?

I have a form to create a Reservation, this form contains an embed form a CollectionType for the form named ResaWeekType corresponding to an Entity called ResaWeek that is a Join Table of the following entities: Resa and Week.

The ResaWeek table also has a field (that’s why I don’t have a ManyToMany relation between Reservation and Week):

class ResaWeek
{
    /**
     * @ORMId()
     * @ORMManyToOne(targetEntity="AppEntityResa", inversedBy="resaWeeks")
     */
    private $resa;

    /**
     * @ORMId()
     * @ORMManyToOne(targetEntity="AppEntityWeek", inversedBy="resaWeeks")
     */
    private $week;

    /**
     * @ORMManyToOne(targetEntity="AppEntityState", inversedBy="resaWeeks")
     */
    private $state;
    // {...}
}

Here is the scheme.

Here is the scheme.

Here is the ReservationType Form:

$builder
    ->add('resaWeeks', CollectionType::class, [
        'entry_type' => ResaWeekType::class,
        'entry_options' => ['label' => false],
        'allow_add' => true,
        'allow_delete' => true,
        'by_reference' => false,
    ])
;

So, the ResaWeekType Form calls an EntityType for Week:

$builder
    ->add('week', EntityType::class, [
        'class' => Week::class
    ])
;

I have added javascripts (based on the documentation) to be able to delete or add a row for a ResaWeek item.

My problem is that there is the possibility to create duplicate of ResaWeek with the same id (since for this you only need to add two ResaWeek and select the same Week field in the form). How can I avoid this situation?

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 1

Your email address will not be published. Required fields are marked *


Miguel

Miguel

Have you tried @UniqueEntity in the entity definition?