我没有使用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%'
]
答案 0 :(得分:0)
如果您不使用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';
}
}
这应该让它发挥作用。