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 @@ tagName.%name%.alreadyExists Tag %name% already exists. + + + + field_with_same_name_already_exists_but_with_different_doctrine_type + There already is a "%name%" field inside "%nodeTypeName%" but with a different data-type: %type%. + diff --git a/lib/RoadizCoreBundle/translations/validators.fr.xlf b/lib/RoadizCoreBundle/translations/validators.fr.xlf index 0daaf454..9e538c0e 100644 --- a/lib/RoadizCoreBundle/translations/validators.fr.xlf +++ b/lib/RoadizCoreBundle/translations/validators.fr.xlf @@ -10,6 +10,11 @@ tagName.%name%.alreadyExists L'étiquette «%name%» existe déjà. + + + field_with_same_name_already_exists_but_with_different_doctrine_type + Un champ « %name% » existe déjà pour « %nodeTypeName% » mais avec un type de données différent : %type%. + diff --git a/lib/RoadizCoreBundle/translations/validators.xlf b/lib/RoadizCoreBundle/translations/validators.xlf index 82fbfc32..413ebfd4 100644 --- a/lib/RoadizCoreBundle/translations/validators.xlf +++ b/lib/RoadizCoreBundle/translations/validators.xlf @@ -10,6 +10,10 @@ tagName.%name%.alreadyExists + + field_with_same_name_already_exists_but_with_different_doctrine_type + +