Validate info provider references modified via part edit form

This commit is contained in:
Jan Böhmer 2026-06-28 23:56:55 +02:00
parent e03eda84c5
commit ffcfdb793f
4 changed files with 58 additions and 1 deletions

View file

@ -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();
}
}
}

View file

@ -75,6 +75,7 @@ trait AdvancedPropertyTrait
*/
#[ORM\Embedded(class: InfoProviderReference::class, columnPrefix: 'provider_reference_')]
#[Groups(['full', 'part:read'])]
#[Assert\Valid()]
protected InfoProviderReference $providerReference;
/**

View file

@ -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);
}

View file

@ -253,5 +253,23 @@
<target>This is not an valid GTIN / EAN!</target>
</segment>
</unit>
<unit id="vnpejmb" name="info_providers.validation.provider_id_without_key">
<segment>
<source>info_providers.validation.provider_id_without_key</source>
<target>If you specify an provider ID, you also need to specify an info provider or remove both.</target>
</segment>
</unit>
<unit id="yFlA5OA" name="info_providers.validation.provider_url_without_key">
<segment>
<source>info_providers.validation.provider_url_without_key</source>
<target>If you specify an provider URL, you also need to specify an info provider.</target>
</segment>
</unit>
<unit id="gUHUXoV" name="info_providers.validation.provider_key_without_id">
<segment>
<source>info_providers.validation.provider_key_without_id</source>
<target>If you specify an info provider, you also need to provide an provider id, or remove both.</target>
</segment>
</unit>
</file>
</xliff>