diff --git a/lib/RoadizCoreBundle/src/Form/Constraint/NodeTypeFieldValidator.php b/lib/RoadizCoreBundle/src/Form/Constraint/NodeTypeFieldValidator.php
index 9ba3b5c0..696252df 100644
--- a/lib/RoadizCoreBundle/src/Form/Constraint/NodeTypeFieldValidator.php
+++ b/lib/RoadizCoreBundle/src/Form/Constraint/NodeTypeFieldValidator.php
@@ -4,6 +4,7 @@
namespace RZ\Roadiz\CoreBundle\Form\Constraint;
+use Doctrine\Persistence\ManagerRegistry;
use RZ\Roadiz\Core\AbstractEntities\PersistableInterface;
use RZ\Roadiz\CoreBundle\Configuration\CollectionFieldConfiguration;
use RZ\Roadiz\CoreBundle\Configuration\JoinNodeTypeFieldConfiguration;
@@ -19,23 +20,46 @@
class NodeTypeFieldValidator extends ConstraintValidator
{
+ public function __construct(
+ private readonly ManagerRegistry $registry,
+ ) {
+ }
+
public function validate(mixed $value, Constraint $constraint): void
{
- if ($value instanceof NodeTypeFieldEntity) {
- if ($value->isMarkdown()) {
- $this->validateMarkdownOptions($value);
- }
- if ($value->isManyToMany() || $value->isManyToOne()) {
- $this->validateJoinTypes($value, $constraint);
- }
- if ($value->isMultiProvider() || $value->isSingleProvider()) {
- $this->validateProviderTypes($value, $constraint);
+ if (!$value instanceof NodeTypeFieldEntity) {
+ $this->context->buildViolation('Value is not a valid NodeTypeField.')->addViolation();
+ return;
+ }
+
+ $existingNodeTypeFieldsByName = $this->registry->getRepository(NodeTypeFieldEntity::class)->findBy([
+ 'name' => $value->getName(),
+ ]);
+ foreach ($existingNodeTypeFieldsByName as $item) {
+ if ($item->getId() === $value->getId()) {
+ continue;
}
- if ($value->isCollection()) {
- $this->validateCollectionTypes($value, $constraint);
+ if ($item->getDoctrineType() !== $value->getDoctrineType()) {
+ $this->context->buildViolation('field_with_same_name_already_exists_but_with_different_doctrine_type')
+ ->setParameter('%name%', $item->getName())
+ ->setParameter('%nodeTypeName%', $item->getNodeTypeName())
+ ->setParameter('%type%', $item->getDoctrineType())
+ ->atPath('name')
+ ->addViolation();
}
- } else {
- $this->context->buildViolation('Value is not a valid NodeTypeField.')->addViolation();
+ }
+
+ if ($value->isMarkdown()) {
+ $this->validateMarkdownOptions($value);
+ }
+ if ($value->isManyToMany() || $value->isManyToOne()) {
+ $this->validateJoinTypes($value, $constraint);
+ }
+ if ($value->isMultiProvider() || $value->isSingleProvider()) {
+ $this->validateProviderTypes($value, $constraint);
+ }
+ if ($value->isCollection()) {
+ $this->validateCollectionTypes($value, $constraint);
}
}
diff --git a/lib/RoadizCoreBundle/translations/validators.en.xlf b/lib/RoadizCoreBundle/translations/validators.en.xlf
index 62c27ea0..c10b9005 100644
--- a/lib/RoadizCoreBundle/translations/validators.en.xlf
+++ b/lib/RoadizCoreBundle/translations/validators.en.xlf
@@ -10,6 +10,12 @@
Tag %name% already exists.
+
+
+
+
+ There already is a "%name%" field inside "%nodeTypeName%" but with a different data-type: %type%.
+