prevent recursion

This commit is contained in:
Julien Fastré 2021-10-04 16:16:34 +02:00
parent 27dea97bc6
commit c41bc7bef9
2 changed files with 27 additions and 1 deletions

View File

@ -7,6 +7,8 @@ namespace Chill\AsideActivityBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
/**
* @ORM\Entity
@ -83,11 +85,32 @@ class AsideActivityCategory
return $this;
}
public function getParent(): ?self
public function getParent(): ?self
{
return $this->parent;
}
/**
*
* @Assert\Callback()
*/
public function preventRecursiveParent(ExecutionContextInterface $context, $payload)
{
if (!$this->hasParent()) {
return;
}
if ($this->getParent() === $this) {
// replace parent with old parent. This prevent recursive loop
// when displaying form
$this->parent = $this->oldParent;
$context->buildViolation('You must not add twice the same category in the parent tree (previous result returned)')
->atPath('parent')
->addViolation()
;
}
}
public function hasParent(): bool
{
return $this->parent !== null;
@ -95,6 +118,8 @@ class AsideActivityCategory
public function setParent(?self $parent): self
{
// cache the old result for changing it during validaiton
$this->oldParent = $this->parent;
$this->parent = $parent;
return $this;

View File

@ -0,0 +1 @@
You must not add twice the same category in the parent tree (previous result returned): Il est interdit d'indiquer la même entité dans l'arbre des parents. Le résultat précédent a été rétabli