Skip to content

Commit

Permalink
feat: Prevent creating same NodeTypeField name but with different doc…
Browse files Browse the repository at this point in the history
…trine type.
  • Loading branch information
ambroisemaupate committed Feb 23, 2024
1 parent d4981f0 commit c81773d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 13 deletions.
50 changes: 37 additions & 13 deletions lib/RoadizCoreBundle/src/Form/Constraint/NodeTypeFieldValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}

Expand Down
6 changes: 6 additions & 0 deletions lib/RoadizCoreBundle/translations/validators.en.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
<source>tagName.%name%.alreadyExists</source>
<target state="translated">Tag %name% already exists.</target>
</trans-unit>


<trans-unit id="field_with_same_name_already_exists_but_with_different_doctrine_type" xml:space="preserve">
<source>field_with_same_name_already_exists_but_with_different_doctrine_type</source>
<target state="translated">There already is a "%name%" field inside "%nodeTypeName%" but with a different data-type: %type%.</target>
</trans-unit>
</body>
</file>
</xliff>
5 changes: 5 additions & 0 deletions lib/RoadizCoreBundle/translations/validators.fr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
<source>tagName.%name%.alreadyExists</source>
<target state="translated">L'étiquette «%name%» existe déjà.</target>
</trans-unit>

<trans-unit id="field_with_same_name_already_exists_but_with_different_doctrine_type" xml:space="preserve">
<source>field_with_same_name_already_exists_but_with_different_doctrine_type</source>
<target state="translated">Un champ « %name% » existe déjà pour « %nodeTypeName% » mais avec un type de données différent : %type%.</target>
</trans-unit>
</body>
</file>
</xliff>
4 changes: 4 additions & 0 deletions lib/RoadizCoreBundle/translations/validators.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
<source>tagName.%name%.alreadyExists</source>
<target />
</trans-unit>
<trans-unit id="field_with_same_name_already_exists_but_with_different_doctrine_type" xml:space="preserve">
<source>field_with_same_name_already_exists_but_with_different_doctrine_type</source>
<target />
</trans-unit>
</body>
</file>
</xliff>

0 comments on commit c81773d

Please sign in to comment.