Added some constraints to ensure validity of the PartAssociation entities

This commit is contained in:
Jan Böhmer 2023-11-14 23:50:09 +01:00
parent c7892cb9e2
commit cc2332a83a
2 changed files with 24 additions and 0 deletions

View file

@ -28,6 +28,7 @@ use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use App\Entity\Base\AbstractDBElement; use App\Entity\Base\AbstractDBElement;
use App\Entity\Base\TimestampTrait; use App\Entity\Base\TimestampTrait;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\Validator\Constraints as Assert;
/** /**
@ -36,6 +37,7 @@ use Symfony\Component\Validator\Constraints as Assert;
*/ */
#[ORM\Entity(repositoryClass: DBElementRepository::class)] #[ORM\Entity(repositoryClass: DBElementRepository::class)]
#[ORM\HasLifecycleCallbacks] #[ORM\HasLifecycleCallbacks]
#[UniqueEntity(fields: ['other', 'owner', 'type'], message: 'validator.part_association.already_exists')]
class PartAssociation extends AbstractDBElement class PartAssociation extends AbstractDBElement
{ {
use TimestampTrait; use TimestampTrait;
@ -51,6 +53,8 @@ class PartAssociation extends AbstractDBElement
* is used if the type is OTHER * is used if the type is OTHER
*/ */
#[ORM\Column(type: Types::STRING, length: 255, nullable: true)] #[ORM\Column(type: Types::STRING, length: 255, nullable: true)]
#[Assert\Expression("this.getType().value !== 0 or this.getOtherType() !== null",
message: 'validator.part_association.must_set_an_value_if_type_is_other')]
protected ?string $other_type = null; protected ?string $other_type = null;
/** /**
@ -73,6 +77,8 @@ class PartAssociation extends AbstractDBElement
#[ORM\ManyToOne(targetEntity: Part::class, inversedBy: 'associated_parts_as_other')] #[ORM\ManyToOne(targetEntity: Part::class, inversedBy: 'associated_parts_as_other')]
#[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')] #[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
#[Assert\NotNull] #[Assert\NotNull]
#[Assert\Expression("this.getOwner() !== this.getOther()",
message: 'validator.part_association.part_cannot_be_associated_with_itself')]
protected ?Part $other = null; protected ?Part $other = null;
public function getType(): AssociationType public function getType(): AssociationType

View file

@ -317,5 +317,23 @@
<target>A lot owner must not be the anonymous user!</target> <target>A lot owner must not be the anonymous user!</target>
</segment> </segment>
</unit> </unit>
<unit id="N8aA0Uh" name="validator.part_association.must_set_an_value_if_type_is_other">
<segment>
<source>validator.part_association.must_set_an_value_if_type_is_other</source>
<target>If you set the type to "other", then you have to set a descriptive value for it!</target>
</segment>
</unit>
<unit id="9VYNZ4v" name="validator.part_association.part_cannot_be_associated_with_itself">
<segment>
<source>validator.part_association.part_cannot_be_associated_with_itself</source>
<target>A part can not be associated with itself!</target>
</segment>
</unit>
<unit id="csc1PNn" name="validator.part_association.already_exists">
<segment>
<source>validator.part_association.already_exists</source>
<target>The association with this part already exists!</target>
</segment>
</unit>
</file> </file>
</xliff> </xliff>