add component and type to add user dynamically

This commit is contained in:
2022-01-03 15:42:24 +01:00
parent 5bf1b9d8bd
commit 45dd21e02a
12 changed files with 334 additions and 6 deletions

View File

@@ -14,6 +14,7 @@ namespace Chill\MainBundle\Form;
use Chill\MainBundle\Entity\Notification;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Form\Type\ChillTextareaType;
use Chill\MainBundle\Form\Type\PickUserDynamicType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
@@ -24,11 +25,8 @@ class NotificationType extends AbstractType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('addressees', EntityType::class, [
'class' => User::class,
'choice_label' => 'label',
->add('addressees', PickUserDynamicType::class, [
'multiple' => true,
'by_reference' => true,
])
->add('message', ChillTextareaType::class, [
'required' => false,

View File

@@ -0,0 +1,70 @@
<?php
namespace Chill\MainBundle\Form\Type\DataTransformer;
use Chill\MainBundle\Entity\User;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\SerializerInterface;
class UserToJsonTransformer implements DataTransformerInterface
{
private SerializerInterface $serializer;
private DenormalizerInterface $denormalizer;
private bool $multiple;
public function __construct(DenormalizerInterface $denormalizer, SerializerInterface $serializer, bool $multiple)
{
$this->denormalizer = $denormalizer;
$this->serializer = $serializer;
$this->multiple = $multiple;
}
/**
* @param User|User[] $value
*/
public function transform($value): string
{
if (null === $value) {
return $this->multiple ? "null" : "[]";
}
return $this->serializer->serialize($value, 'json', [
AbstractNormalizer::GROUPS => ['read'],
]);
}
public function reverseTransform($value)
{
if ($this->multiple) {
return \array_map(
function($item) { return $this->denormalizeOne($item);},
json_decode($value, true)
);
}
return $this->denormalizeOne(json_decode($value, true));
}
private function denormalizeOne(array $item): User
{
if (!array_key_exists('type', $item)) {
throw new TransformationFailedException('the key "type" is missing on element');
}
if (!array_key_exists('id', $item)) {
throw new TransformationFailedException('the key "id" is missing on element');
}
return
$this->denormalizer->denormalize(
['type' => $item['type'], 'id' => $item['id']],
User::class,
'json',
[AbstractNormalizer::GROUPS => ['read']],
);
}
}

View File

@@ -0,0 +1,59 @@
<?php
namespace Chill\MainBundle\Form\Type;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Form\Type\DataTransformer\UserToJsonTransformer;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerInterface;
/**
* Pick user dymically, using vuejs module "AddPerson"
*/
class PickUserDynamicType extends AbstractType
{
private SerializerInterface $serializer;
private DenormalizerInterface $denormalizer;
public function __construct(DenormalizerInterface $denormalizer, SerializerInterface $serializer)
{
$this->denormalizer = $denormalizer;
$this->serializer = $serializer;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addViewTransformer(new UserToJsonTransformer($this->denormalizer, $this->serializer, $options['multiple']));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver
->setDefault('multiple', false)
->setAllowedTypes('multiple', ['bool'])
->setDefault('compound', false)
;
}
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars['multiple'] = $options['multiple'];
$view->vars['types'] = ['user'];
$view->vars['uniqid'] = uniqid('pick_user_dyn');
}
public function getBlockPrefix()
{
return 'pick_user_dynamic';
}
}