[英]Symfony 3.2 FOSMessageBundle '…/new' (ie compose new message) throws error no matter what I do
我沒有使用FOSUserBundle(已經有自定義用戶/安全代碼)。 我一直在這里關注文檔。
經過大量的谷歌搜索和挖掘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%'
]
如果您不使用FOSUSerBundle,則需要執行以下操作:
在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
注冊服務:
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 }
創建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'; } }
創建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'; } }
這應該讓它工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.