diff --git a/src/Entity/Parts/InfoProviderReference.php b/src/Entity/Parts/InfoProviderReference.php index 0ba04c25..c923ad74 100644 --- a/src/Entity/Parts/InfoProviderReference.php +++ b/src/Entity/Parts/InfoProviderReference.php @@ -28,6 +28,8 @@ use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping\Column; use Doctrine\ORM\Mapping\Embeddable; use Symfony\Component\Serializer\Annotation\Groups; +use Symfony\Component\Validator\Constraints as Assert; +use Symfony\Component\Validator\Context\ExecutionContextInterface; /** * This class represents a reference to an info provider inside a part. @@ -159,7 +161,14 @@ class InfoProviderReference return $ref; } - + /** + * Creates a reference to an info provider based on the given parameters. + * @param string|null $provider_key + * @param string|null $provider_id + * @param string|null $provider_url + * @param \DateTimeImmutable|null $last_updated + * @return self + */ public static function create(?string $provider_key, ?string $provider_id, ?string $provider_url, ?\DateTimeImmutable $last_updated): self { $ref = new InfoProviderReference(); @@ -169,4 +178,26 @@ class InfoProviderReference $ref->last_updated = $last_updated; return $ref; } + + #[Assert\Callback()] + public function validate(ExecutionContextInterface $context, mixed $payload): void + { + if ($this->provider_key === null && $this->provider_id !== null) { + $context->buildViolation('info_providers.validation.provider_id_without_key') + ->atPath('provider_key') + ->addViolation(); + } + + if ($this->provider_key === null && $this->provider_url !== null) { + $context->buildViolation('info_providers.validation.provider_url_without_key') + ->atPath('provider_url') + ->addViolation(); + } + + if ($this->provider_key !== null && $this->provider_id === null) { + $context->buildViolation('info_providers.validation.provider_key_without_id') + ->atPath('provider_id') + ->addViolation(); + } + } } diff --git a/src/Entity/Parts/PartTraits/AdvancedPropertyTrait.php b/src/Entity/Parts/PartTraits/AdvancedPropertyTrait.php index 065469b5..9fa41f93 100644 --- a/src/Entity/Parts/PartTraits/AdvancedPropertyTrait.php +++ b/src/Entity/Parts/PartTraits/AdvancedPropertyTrait.php @@ -75,6 +75,7 @@ trait AdvancedPropertyTrait */ #[ORM\Embedded(class: InfoProviderReference::class, columnPrefix: 'provider_reference_')] #[Groups(['full', 'part:read'])] + #[Assert\Valid()] protected InfoProviderReference $providerReference; /** diff --git a/src/Form/InfoProviderSystem/InfoProviderReferenceType.php b/src/Form/InfoProviderSystem/InfoProviderReferenceType.php index 4a173a04..73fc8fe3 100644 --- a/src/Form/InfoProviderSystem/InfoProviderReferenceType.php +++ b/src/Form/InfoProviderSystem/InfoProviderReferenceType.php @@ -100,6 +100,13 @@ class InfoProviderReferenceType extends AbstractType implements DataMapperInterf } $oldDate = $viewData->getLastUpdated(); + + //If all fields are empty, we set the view data to a new instance without provider information + if ($providerKey === null && $providerId === null && $providerUrl === null) { + $viewData = InfoProviderReference::noProvider(); + return; + } + $viewData = InfoProviderReference::create($providerKey, $providerId, $providerUrl, $oldDate); } diff --git a/translations/validators.en.xlf b/translations/validators.en.xlf index 624c6a89..1a60088e 100644 --- a/translations/validators.en.xlf +++ b/translations/validators.en.xlf @@ -253,5 +253,23 @@ This is not an valid GTIN / EAN! + + + info_providers.validation.provider_id_without_key + If you specify an provider ID, you also need to specify an info provider or remove both. + + + + + info_providers.validation.provider_url_without_key + If you specify an provider URL, you also need to specify an info provider. + + + + + info_providers.validation.provider_key_without_id + If you specify an info provider, you also need to provide an provider id, or remove both. + +