<?php
declare(strict_types=1);
namespace Juki\Bundle\AppBundle\EventListener;
use Doctrine\ORM\EntityManagerInterface;
use Hitso\Bundle\FormBundle\Event\FormEvent;
use Hitso\Bundle\FormBundle\Form\Type\ImageType;
use Juki\Bundle\AppBundle\Entity\Dictionary\Shop;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Validator\Constraints\Callback;
use Symfony\Component\Validator\Constraints\Count;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
/**
* Class CategorySubscriber
*
* @package Juki\Bundle\AppBundle\EventListener
*/
final class CategorySubscriber implements EventSubscriberInterface
{
protected $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
public static function getSubscribedEvents()
{
return [
'category_type.form_init' => 'onCategoryFormInit',
];
}
public function onCategoryFormInit(FormEvent $event)
{
$builder = $event->getBuilder();
$builder->add(
'shops',
ChoiceType::class,
[
'label' => 'Sklep',
'required' => true,
'translation_domain' => 'admin',
'multiple' => false,
'expanded' => false,
'choices' => $this->entityManager->getRepository(Shop::class)->getParentChoices(),
'position' => ['before' => 'name'],
// 'constraints' => [
// new Count([
// 'min' => 1,
// 'minMessage' => 'Musisz wybrać przynajmniej 1 sklep',
// ]),
// ],
]
);
$builder->add(
'shortName',
TextType::class,
[
'label' => 'Nazwa skrócona',
'required' => false,
'empty_data' => '',
'position' => ['after' => 'name'],
'constraints' => [new Length(['max' => 255])],
]
);
$builder->add(
'navigationIcon',
ImageType::class,
[
'label' => 'Ikona nawigacji (SVG)',
'config_name' => 'icon_attachment',
'required' => false,
'position' => ['after' => 'photo'],
]
);
}
}