【问题标题】:Symfony 3.2 FOSMessageBundle '.../new' (ie compose new message) throws error no matter what I doSymfony 3.2 FOSMessageBundle '.../new'(即撰写新消息)无论我做什么都会抛出错误
【发布时间】:2017-03-30 06:19:05
【问题描述】:

我没有使用 FOSUserBundle(已经有自定义用户/安全代码)。我一直在关注文档here

在对 Symfony 和 FOSMessageBundle 文件进行大量搜索和挖掘之后,我开始相信这个问题与新的 NewThreadMessageFormFactory 类有关。它只有一种方法。如果我保持原样

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

我收到此错误Expected argument of type "string", "AppBundle\Service\NewThreadMessageFormType" given。另一方面,如果我执行以下操作,

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

我知道了Could not load type "FOS\UserBundle\Form\Type\UsernameFormType。当然,它甚至不存在,因为 FOSUserBundle 甚至没有安装。

我已经为此花费了十几个小时。我在这个过程中学到了很多东西,但我仍然无法让这该死的东西发挥作用......


还有我目前的配置

//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

还有……

  //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%'
          ]

【问题讨论】:

    标签: php symfony yaml symfony-3.2


    【解决方案1】:

    如果您不使用 FOSUSerBundle,则需要执行以下操作:

    1. 按照指南Using other UserBundles 创建 UserToUsernameTransformer 类
    2. 在 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. 注册服务:

      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. 创建一个 NewThreadMessageFormType 表单类型类来覆盖默认的。

      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. 创建一个 UsernameFormType 表单类型类来处理用户名的转换和显示。

      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';
          }
      }
      

    这应该可以正常工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-11
      • 2011-04-26
      • 2017-11-21
      • 1970-01-01
      • 2010-12-04
      • 1970-01-01
      相关资源
      最近更新 更多