Add all and none special tags
This commit is contained in:
@ -8,15 +8,19 @@ use Doctrine\Common\Collections\Collection;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\DataTransformerInterface;
|
||||
use Symfony\Component\Form\Exception\TransformationFailedException;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
use Symfony\Component\Validator\ConstraintViolationInterface;
|
||||
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
||||
|
||||
class TagsType extends AbstractType implements DataTransformerInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly TagRepository $repository,
|
||||
private readonly Security $security,
|
||||
private readonly TagRepository $repository,
|
||||
private readonly Security $security,
|
||||
private readonly ValidatorInterface $validator,
|
||||
) {}
|
||||
|
||||
public function transform($value): string
|
||||
@ -46,10 +50,20 @@ class TagsType extends AbstractType implements DataTransformerInterface
|
||||
foreach ($tags as $tag) {
|
||||
$tagEntity = $this->repository->findOneBy(['name' => $tag, 'user' => $user]);
|
||||
if ($tagEntity === null) {
|
||||
$tagEntity = new Tag()
|
||||
->setName($tag)
|
||||
->setUser($user)
|
||||
;
|
||||
$tagEntity = new Tag();
|
||||
$tagEntity->setName($tag)->setUser($user);
|
||||
|
||||
// Validate the new Tag entity
|
||||
$errors = $this->validator->validate($tagEntity);
|
||||
if (count($errors) > 0) {
|
||||
$exception = new TransformationFailedException();
|
||||
$exception->setInvalidMessage(implode(', ', array_map(
|
||||
fn(ConstraintViolationInterface $error) => $error->getMessage(),
|
||||
iterator_to_array($errors)
|
||||
)));
|
||||
throw $exception;
|
||||
}
|
||||
|
||||
$this->repository->save($tagEntity);
|
||||
}
|
||||
$tagEntities[] = $tagEntity;
|
||||
|
Reference in New Issue
Block a user