dotelauv682684 2017-03-30 06:19
浏览 70
已采纳

Symfony 3.2 FOSMessageBundle'... / new'(即撰写新消息)无论我做什么都会抛出错误

I'm not using FOSUserBundle (there's custom user/security code already in place). I've been following the docs here.

After much googling and digging through both Symfony and FOSMessageBundle files, I've come to believe that the issue has something to do with the new NewThreadMessageFormFactory class. It only has one method. If I leave it as is like so

    public function create()
    {
        return $this->formFactory->createNamed(
          $this->formName, 
          $this->formType, 
          $this->createModelInstance());
    }

I get this error Expected argument of type "string", "AppBundle\Service\NewThreadMessageFormType" given. On the other hand, if I do the following,

public function create()
{
    return $this->formFactory->createNamed(
      $this->formName,
      'AppBundle\Service\NewThreadMessageFormType', 
      $this->createModelInstance());
}

I get this Could not load type "FOS\UserBundle\Form\Type\UsernameFormType. Which, of course, doesn't even exist because FOSUserBundle isn't even installed.

I've already spent more than a dozen hours on this. I've learned a lot in the process, but I still can't make the darn thing work...


And my current configuration

//config.yml
fos_message:
    db_driver: orm
    thread_class: AppBundle\Entity\Thread
    message_class: AppBundle\Entity\Message
    new_thread_form:
        type: app.new_thread_form_type
        factory: app.new_thread_form_factory

and...

  //services.yml
  fos_user.user_to_username_transformer:
      alias: app.user_to_username_transformer

  app.user_to_username_transformer:
      class: AppBundle\Form\DataTransformer\UserToUsernameTransformer
      arguments:
          type: "service"
          id: "doctrine"

  app.new_thread_form_type:
      class: AppBundle\Service\NewThreadMessageFormType
      arguments: ['@app.user_to_username_transformer']
      tags:
          - {name: form.type}

  app.new_thread_form_factory:
      class: AppBundle\Service\NewThreadMessageFormFactory
      arguments: [
          '@form.factory',
          '@fos_message.new_thread_form.type',
          '%fos_message.new_thread_form.name%',
          '%fos_message.new_thread_form.model%'
          ]
  • 写回答

1条回答 默认 最新

  • douna2014 2017-04-08 23:17
    关注

    If you are not using the FOSUSerBundle, then you need to do the following:

    1. Create the UserToUsernameTransformer class as in the guide Using other UserBundles
    2. In config.yml:

      fos_message:
          db_driver: orm
          thread_class: AppBundle\Entity\Thread
          message_class: AppBundle\Entity\Message
          new_thread_form:
              type: AppBundle\Form\Type\NewThreadMessageFormType
      
    3. Register the services:

      app.user_to_username_transformer:
          class: AppBundle\Form\DataTransformer\UserToUsernameTransformer
          arguments: ['@doctrine']
      fos_user.user_to_username_transformer:
          alias: app.user_to_username_transformer
      app.form.type.username:
          class: AppBundle\Form\Type\UsernameFormType
          arguments: ['@app.user_to_username_transformer']
          tags:
              - { name: form.type }
      
    4. Create a NewThreadMessageFormType form type class to override the default one.

      class NewThreadMessageFormType extends AbstractType
      {
          public function buildForm(FormBuilderInterface $builder, array $options)
          {
              $builder
                  ->add('recipient', UsernameFormType::class, array(
                      'label' => 'recipient',
                      'translation_domain' => 'FOSMessageBundle',
                  ))
              ->add('subject', TextType::class, array(
                  'label' => 'subject',
                  'translation_domain' => 'FOSMessageBundle',
              ))
              ->add('body', TextareaType::class, array(
                  'label' => 'body',
                  'translation_domain' => 'FOSMessageBundle',
              ));
          }
      
          public function configureOptions(OptionsResolver $resolver)
          {
              $resolver->setDefaults(array(
                  'intention' => 'message',
              ));
          }
      
          /**
           * {@inheritdoc}
           */
          public function getBlockPrefix()
          {
              return 'new_thread_message';
          }
      }
      
    5. Create a UsernameFormType form type class to handle transforming and displaying the username.

      class UsernameFormType extends AbstractType
      {
          /**
           * @var UserToUsernameTransformer
           */
          protected $usernameTransformer;
      
          /**
           * Constructor.
           *
           * @param UserToUsernameTransformer $usernameTransformer
           */
          public function __construct(UserToUsernameTransformer $usernameTransformer)
          {
              $this->usernameTransformer = $usernameTransformer;
          }
      
          /**
           * {@inheritdoc}
           */
          public function buildForm(FormBuilderInterface $builder, array $options)
          {
              $builder->addModelTransformer($this->usernameTransformer);
          }
      
          /**
           * {@inheritdoc}
           */
          public function getParent()
          {
              return TextType::class;
          }
      
          /**
           * {@inheritdoc}
           */
          public function getBlockPrefix()
          {
              return 'toolsets_username_type';
          }
      }
      

    That should get it working.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后的密码
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面