FriendsOfSymfony / FOSMessageBundle

User-to-user messaging bundle for Symfony
356 stars 183 forks source link

Is it possible to use a ChoiceType instead of a TextType for recipients ? #297

Closed charafsalmi closed 5 years ago

charafsalmi commented 7 years ago

I am struggling trying to use a Select Input Field to display a Select2 widget instead of the Text Input Field.

I add new services in services.yml :

  app.type.recipients_selector:
    class: AppBundle\Form\Type\RecipientsType
    arguments: ['@app.type.recipients_data_transformer']
    tags:
        - { name: form.type }
  app.type.recipients_data_transformer:
    class: AppBundle\DataTransformer\RecipientsDataTransformer
    arguments: ['@app.type.user_to_username_transformer']
  app.type.user_to_username_transformer:
    class: AppBundle\DataTransformer\UserToUsernameTransformer
    arguments: ['@fos_user.user_manager']

And config.xml

fos_message:
    db_driver: orm
    thread_class: AppBundle\Entity\Thread
    message_class: AppBundle\Entity\Message
    new_thread_form:
      type:               AppBundle\Form\Type\NewThreadMultipleMessageFormType
      handler:            fos_message.new_thread_multiple_form.handler
      model:              AppBundle\Form\Model\NewThreadMultipleMessage
      name:               message

Then, I dupplicated these services' files in the right places.

I also created a JSON Controller to give back the filtered list of users ID I want my Select2 to display.

But when I try to send a message to the right user, it says that this user doesn't exist.

I can not figure out how the data transformers handle the information in this case.

The RecipientsDataTransformer->transform method allows get an empty array as a parameter. How is that possible when the select input field returns an ID ?

What can I do more ?

Thank you.

charafsalmi commented 7 years ago

I forgot to add :

<?php

namespace AppBundle\Form\Type;

use FOS\MessageBundle\Util\LegacyFormHelper;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\AbstractType;
use \AppBundle\Form\Type\RecipientsType;

/**
 * Message form type for starting a new conversation with multiple recipients.
 *
 * @author Łukasz Pospiech <zocimek@gmail.com>
 */
class NewThreadMultipleMessageFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('recipients', RecipientsType::class, array(
                'label' => 'recipients',
                'translation_domain' => 'FOSMessageBundle',
            ))
            ->add('subject', LegacyFormHelper::getType('Symfony\Component\Form\Extension\Core\Type\TextType'), array(
                'label' => 'subject',
                'translation_domain' => 'FOSMessageBundle',
            ))
            ->add('body', LegacyFormHelper::getType('Symfony\Component\Form\Extension\Core\Type\TextareaType'), array(
                'label' => 'body',
                'translation_domain' => 'FOSMessageBundle',
            ));
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'fos_message_new_multiperson_thread';
    }

    /**
     * @deprecated To remove when supporting only Symfony 3
     */
    public function getName()
    {
        return $this->getBlockPrefix();
    }
}

<?php

namespace AppBundle\Form\Type;

use FOS\MessageBundle\Util\LegacyFormHelper;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use AppBundle\DataTransformer\RecipientsDataTransformer;

use Symfony\Component\Form\Extension\Core\Type\ChoiceType;

/**
 * Description of RecipientsType.
 *
 * @author Łukasz Pospiech <zocimek@gmail.com>
 */
class RecipientsType extends AbstractType
{
    /**
     * @var RecipientsDataTransformer
     */
    private $recipientsTransformer;

    /**
     * @param RecipientsDataTransformer $transformer
     */
    public function __construct(RecipientsDataTransformer $transformer)
    {
        $this->recipientsTransformer = $transformer;
    }

    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->addModelTransformer($this->recipientsTransformer);
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'invalid_message' => 'no user !',
        ));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $this->configureOptions($resolver);
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'recipients_selector';
    }

    /**
     * {@inheritdoc}
     */
    public function getParent()
    {
        return ChoiceType::class;
    }

    /**
     * @deprecated To remove when supporting only Symfony 3
     */
    public function getName()
    {
        return $this->getBlockPrefix();
    }
}
hex333ham commented 5 years ago

with the way the bundle is constructed, you'd need to create some custom logic for this

however, as this issue is over two years old and I'm tidying up this bundle, if it's still relevant please re-open the issue